I'm trying to create a function to export multiple list elements to an excel workbook with different sheets.
I get the error:
"Error in if (nchar(sheetName) > 31) { : missing value where TRUE/FALSE needed".
My attempt is below:
library("openxlsx")
data <- list("ABCD", "EFGH")
names(data) <- c("X", "Y")
#x = list, y = file name
createWorksheet <- function(x, y){
wb <- createWorkbook()
for(i in x){
addWorksheet(wb, names(x)[i])
writeData(wb, sheetName = names(x)[i], x[[i]])
saveWorkbook(wb, file = y, overwrite = TRUE)
}
}
createWorksheet(data, "data.xlsx")
Any idea what is wrong?