gpt4 book ai didi

r - Openxlsx 包 - 在一张纸上写多个表?

转载 作者:行者123 更新时间:2023-12-04 10:07:20 25 4
gpt4 key购买 nike

一张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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com