As the title suggest, I would like to do the above. Example below:
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)))
The last line is where it throws up an error:
Error: IllegalArgumentException (Java): Sheet index (-1) is out of range (0..9)
To troubleshoot this, I tried:
lapply(df.list[1:length(df.list)], function(i) print(names(i)))
And realise that the names of the columns were being passed to the sheet variable. Any ideas how to overcome this?
You need to
lapplyover the index instead of the list: