正如标题所示,我想做上面的事情。下面的例子:
library(stringr)
library(XLConnect)
df <- data.frame(do.call("rbind", lapply(1:10, function(i) rnorm(10))))
df.list <- rep(list(df), 10)
names(df.list) <- paste("DataFrame", str_pad(1:length(df.list), 2, pad = "0"), sep = "")
df.list.workbook <- loadWorkbook("df.list.workbook.xlsx", create = TRUE)
lapply(1:length(df.list), function(i) createSheet(df.list.workbook, name = names(df.list[i])))
lapply(df.list[1:length(df.list)], function(i) writeWorksheet(df.list.workbook, i, sheet = names(i)))
最后一行是它引发错误的地方:
Error: IllegalArgumentException (Java): Sheet index (-1) is out of range (0..9)
为了解决这个问题,我尝试了:
lapply(df.list[1:length(df.list)], function(i) print(names(i)))
并意识到列的名称被传递给工作表变量。任何想法如何克服这个问题?