0

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?

makeyourownmaker
  • 1,558
  • 2
  • 13
  • 33
user13589693
  • 369
  • 1
  • 8

1 Answers1

0

With Allan's help:

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 seq_along(x)){
    addWorksheet(wb, names(x)[i])
    writeData(wb, sheet = names(x)[i], x[[i]])
    saveWorkbook(wb, file = y, overwrite = TRUE)
  }
}
createWorksheet(data, "data.xlsx")
user13589693
  • 369
  • 1
  • 8