导出列表中的多个数据以分隔的功能 Excel sheet

Function to export multiple data in list to separate Excel sheet

我正在尝试创建一个函数以将多个列表元素导出到具有不同工作表的 excel 工作簿。

我收到错误:

"Error in if (nchar(sheetName) > 31) { : missing value where TRUE/FALSE needed".

我的尝试如下:

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")

知道哪里出了问题吗?

在 Allan 的帮助下:

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")