英文:
write_xlsx results in error: "Argument x must be a data frame or list of data frames" This despite data being a list of DFs
问题
我需要写一个包含多个工作表的Excel文件,工作表的名称分别为"Cases"、"Coordinators"和"Contacts"。
我已经检查了每个工作表的类别,例如class(Cases),在每种情况下的结果如下:
class(Cases)
[1] "tbl_df" "tbl" "data.frame"
我将这些数据框组合成一个名为"compiledData"的列表,如下所示:
compiledData <- c(Cases, Coordinators, Contacts)
并且检查了compiledData的类别,如下所示:
class(compiledData)
[1] "list"
因此,我对以下代码导致以下错误感到困惑:
write_xlsx(compiledData, "FileName.xlsx")
Error in write_xlsx(compiledData, "FileName.xlsx") :
Argument x must be a data frame or list of data frames
非常感谢任何帮助。我已经搜索了两天,但没有找到答案。如果已经发布了解决方案而我没有看到,我提前道歉。
一个更新:我将compiledData <- c(Cases, Coordinators, Contacts)
更改为:
compiledData <- list(Cases, Coordinators, Contacts)
,现在Excel文件正在创建而没有错误。我的新问题是Excel文件中的工作表未标记为"Cases"、"Coordinators"和"Contacts",而是标记为Sheet1、Sheet2、Sheet3。
英文:
I need to write an excel file with multiple sheets named "Cases", "Coordinators", "Contacts"
I have checked the class of each--e.g., class(Cases)--and the result in each case is:
class(Cases)
[1] "tbl_df" "tbl" "data.frame"
I combine these dfs into a list called "compiledData" as follows:
compiledData <- c(Cases, Coordinators, Contacts)
and checked class of compiledData as follows:
class(compiledData)
[1] "list"
So, I am confused as to why the following code results in the following error:
write_xlsx(compiledData, "FileName.xlsx")
Error in write_xlsx(compiledData, "FileName.xlsx") :
Argument x must be a data frame or list of data frames
Any assistance would be greatly appreciated. I have been searching for two days for a solution, but have not found an answer. Apologies in advance if a solution has already been posted and I've not seen it.
One update: I changed compiledData <- c(Cases, Coordinators, Contacts)
to:
compiledData <- list(Cases, Coordinators, Contacts)
and the Excel file is now being created without error. My new problem is that the worksheets in the excel file are not labeled "Cases", "Coordinators", "Contacts"--they are Sheet1, Sheet2, Sheet3
答案1
得分: 0
以下是代码的翻译部分:
当你将数据框存储在列表中(例如,compiledData <- list(Cases, Coordinators, Contacts)
),元素是没有命名的。例如,在这个示例中:
df1 <- df2 <- df3 <- tibble::tibble(A = 1:5, B = 1:5, C = 1:5)
comb <- list(df1, df2, df3)
names(comb)
# NULL
要以数据框名称导出到Excel,你只需要给列表中的元素命名(使用 names()
或 setNames()
):
df1 <- df2 <- df3 <- tibble::tibble(A = 1:5, B = 1:5, C = 1:5)
comb <- list(df1, df2, df3)
names(comb) <- c("df1", "df2", "df3")
writexl::write_xlsx(comb, "test.xlsx")
# 或者一行代码实现
writexl::write_xlsx(setNames(comb, c("df1", "df2", "df3")), "test.xlsx")
注意:因为你已经解决了最初的问题(有关正确导出的问题),但编辑了提出一个新问题(关于命名工作表),请编辑你的问题以删除原始问题,仅关注新问题。这将有助于后续读者。祝好运!
英文:
When you store the data frames in a list (ie, compiledData <- list(Cases, Coordinators, Contacts)
) the elements are unnamed. For instance, in this example:
df1 <- df2 <- df3 <- tibble::tibble(A = 1:5, B = 1:5, C = 1:5)
comb <- list(df1, df2, df3)
names(comb)
# NULL
To export the to excel with the df names, you just have to name the elements in the list (ie, using names()
or setNames()
):
df1 <- df2 <- df3 <- tibble::tibble(A = 1:5, B = 1:5, C = 1:5)
comb <- list(df1, df2, df3)
names(comb) <- c("df1", "df2", "df3")
writexl::write_xlsx(comb, "test.xlsx")
# or one-liner
writexl::write_xlsx(setNames(comb, c("df1", "df2", "df3")), "test.xlsx")
Note: since you resolved your initial question (RE: properly exporting) but edited to pose a new one (RE: naming sheets), please edit your question to remove the original question and only focus on the new one. This will help future readers. Good luck!
答案2
得分: 0
我取消了对"compiledData"的创建
(compiledData <- c(Cases, Coordinators, Contacts)
而是通过以下方式成功地编写了具有命名工作表的Excel文件:
write_xlsx(list(Cases = Cases, Coordinators = Coordinators, Contacts = Contacts), "test.xlsx")
这有点啰嗦,也许有更好的方法,但它有效
英文:
I did away with the creation of "compiledData"
(compiledData <- c(Cases, Coordinators, Contacts)
And instead accomplished the writing of the excel file with named worksheets like this:
write_xlsx(list(Cases = Cases, Coordinators = Coordinators, Contacts = Contacts), "test.xlsx"
)
It is a bit verbose and maybe a better way to do it, but it works
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论