0

I am looking into exporting multiple data frames into the same excel worksheet. Any idea how I can do this?

write.xlsx(test_dataframe, file = "D:/test.xlsx",sheetName = "sheet1", append = TRUE)

I have tried the above command. But all this does is export the data frame to it's own worksheet. So for example if I am creating 4 data frames in an RMD file, I get 4 excel worksheets. I want to be able to get the 4 data frames into the same excel worksheet.

The reason I want to achieve this is my data frames are actually pivot tables that I built in R. Any help on this is much appreciated. Visually this is what I am trying to achieve

RLearner
  • 41
  • 1
  • 9
  • No actually. The one above talks about exporting multiple data frames to multiple worksheets. I am looking to export multiple data frames to the same worksheet. if it is possible? – RLearner Dec 18 '17 at 18:33
  • Have you tried package [XLConnect](https://cran.r-project.org/web/packages/XLConnect/index.html)? – Rui Barradas Dec 18 '17 at 18:40
  • Do you have a preference of *how* they are in the same sheet? Like, do you want them in adjacent columns, or adjacent rows? Do you want blank columns or rows in between? Does each one need to have separate column names? Do the datasets happen to have the same number of columns or rows? This would be a much better question with a small concrete example. Built-in data would be great *if* it matches your use-case closely enough. Would the first 5 rows of `mtcars` and the first 10 rows of `iris` be a decent example? – Gregor Thomas Dec 18 '17 at 18:44
  • They need to be in the same worksheet. one after the other with blank rows separating the data frames/tables. And each table should have a column names. – RLearner Dec 18 '17 at 18:47
  • @RuiBarradas I looked into it after you pointed me there. It looks like something that might be the solution. If I were to use the XLConnect package then I would the writeWorksheet() function to write to excel right? Looks straight forward. But any Idea how I can get the 2 nd table to write by doing something like previous row +2? – RLearner Dec 18 '17 at 18:55
  • @Gregor yes mtcars or the iris data set will do. – RLearner Dec 18 '17 at 19:00
  • 2
    You can use `XLConnect::appendWorksheet` (which doesn't skip any lines) or you can use `XLConnect::getLastRow` and add 2 to it before using `writeWorksheet`. (Or use R to keep track of the number of rows written so far.) – Gregor Thomas Dec 18 '17 at 19:09

0 Answers0