Reputation: 823
I have two data tables that I want to write on two separate sheets in one XLSX file using openxlsx
package. I can write one of them and then load the workbook to add the other table, but that complicates matters (I am also assigning dynamic names to the XLSX files).
However, currently I am unable to initiate a blank workbook (to add the sheets one by one), and cannot find any solution anywhere. Probably I am missing something very trivial, but presently I am at my wit's end. Please help.
Solutions based on openxlsx
will be much appreciated.
Upvotes: 0
Views: 675
Reputation: 11955
library(openxlsx)
df1 = data.frame(ID = c("a","b","c","d"), a = 1:4)
df2 = data.frame(ID = c("p","q","r"), b = 10:12)
wb <- createWorkbook()
addWorksheet(wb, "sheet1")
addWorksheet(wb, "sheet2")
writeDataTable(wb, "sheet1", x = df1, rowNames = F)
writeDataTable(wb, "sheet2", x = df2, rowNames = F)
saveWorkbook(wb, "writeDataTableExample.xlsx", overwrite = TRUE)
#install Rtools in case it throws error
Upvotes: 2