一张excel表格可以写多个表格吗?根据我的尝试,我看到只有最后一个对象被写入文件。可能是我每次都覆盖文件。所以我想知道如何修改代码以在同一张表中写入两个表?谢谢

data.sets <- ls("package:datasets")[c(5,7)]# list of data set names
foonames <- paste0("foo", seq_along(data.sets))
count <- length(data.sets)
my.list <- list()
for (i in 1:count) my.list[[foonames[i]]] <- get(data.sets[[i]])
lapply(my.list, dim)

filen<-sprintf('H:\\11_Analysis\\01_Eye Study\\Output\\Eyestudy1_%s.xlsx',
               Sys.Date());

library(openxlsx)
options("openxlsx.dateFormat" = "yyyy-mm-dd")

wb<-write.xlsx(my.list$foo1, filen,
               startCol =2, startRow = 1,
               asTable = T, withFilter = T,
               colWidths="auto")

wb<-write.xlsx(my.list$foo1, filen,
               startCol =2, startRow = 15,
               asTable = T, withFilter = T,
               colWidths="auto")

最佳答案

当您想将多个表写入同一张表时,您应该创建工作簿,然后将数据写入其中,然后保存。你这样做的方式似乎是覆盖数据。这是我会做的:

wb <- createWorkbook()
addWorksheet(wb, sheetName = "sheet1")

writeData(wb, sheet = 1, x = df, startCol = 2, startRow = 2)
writeData(wb, sheet = 1, x = df2, startCol = 2, startRow = 24)

saveWorkbook(wb, file = "path/to/file.xslx")

关于r - Openxlsx 包 - 在一张纸上写多个表?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47024178/

10-12 17:32