Update XLS spreadsheet without overwriting data using R

Asked

Viewed 193 times

4

I need to add new information to the xls spreadsheet, but this already has information that was previously inserted. I need that data to be on the same sheet.

There is the possibility to update a spreadsheet without overwriting the data that already exist?

I am using write.xls as simply as possible:

write.xlsx(obj, path, sheetName="Dados") 

I tried to use the append, but it always overrides or creates the data on a different sheet. There’s another way to do this?

  • 1

    Do you have to necessarily save in . xlsx? It is much more practical to work with outputs in . csv, in R.

  • I’ll test, @Molx. I don’t have to use xlsx.

  • The problem is that not all my data has the same amounts of lines. For example, I have a set with 3 lines. Then I need to add another set with 4 lines.. the only thing in common is that everyone has information (x,y). But then when I try to use . csv he complains that the amount of information is not compatible.

  • You can append using . csv regardless of the number of lines. Maybe the problem is in trying to write headers or Row Numbers, hence the error. It might be a good idea to consider such an approach next time, as this problem is solved.

  • I’ll test, even if the other one worked, @Molx :)

  • @Molx worked super well even and is much simpler! : ) Thank you so much for your good ideas.

  • Great! R does not like xls, so it is good to avoid, especially as output. Text files are simpler and easier to work with.

Show 2 more comments

1 answer

3


If your data isn’t too large, I would create a function like this:

write.xlsx2 <- function(obj, path, sheetName){
  dados <- read.xlsx(path = path, sheetName = sheetName)
  dados <- rbind(dados, obj)
  write.xlsx(dados, path = path, sheetName = sheetName)   
}
  • I read the data that is already in excel p/ o R
  • Inside the R, I stack them both
  • write all over again

It’s not the safest way, but it should work in most cases.

  • Cool! I liked the idea! It’s functional :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.