仍然没有可复制的例子,但是从你的class(q1)
看起来q1
is a tbl_df
(数据框的类型dplyr
包产生)而write.xlsx
期望一个data.frame
.
尝试给予write.xlsx
一个平原data.frame
正如它所期望的那样。例如
write.xlsx(as.data.frame(q1), ...)
这是一个可重现的例子 https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example(即您可以将其复制粘贴到 R 会话中以重现错误并修复)。
library(dplyr)
iris2 <- tbl_df(iris)
class(iris2) # like yours
# [1] "tbl_df" "tbl" "data.frame"
# Now let's try to write to XLSX using command as mentioned in your comments
library(xlsx)
write.xlsx(iris2, file='test.xlsx', sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=TRUE)
# Error in .jcall(cell, "V", "setCellValue", value) :
# method setCellValue with signature ([D)V not found
# In addition: Warning message:
# In if (is.na(value)) { :
# the condition has length > 1 and only the first element will be used
# ^--- we can reproduce your error. This is the point of a reproducible example, so we can see if our fixes work for you.
现在让我们尝试通过确保 write.xlsx 获取 data.frame 而不是 tbl_df 来修复它!
write.xlsx(as.data.frame(iris2), file='test.xlsx', sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=TRUE)
# huzzah!