Error in printing data.frame in excel using XLSX package in R

坚强是说给别人听的谎言 提交于 2019-12-03 04:18:52
mathematical.coffee

Still no reproducible example, but from your class(q1) it appears that q1 is a tbl_df (the sort of dataframe that the dplyr package produces) whereas write.xlsx expects a data.frame.

Try giving write.xlsx a plain data.frame as it expects. e.g.

write.xlsx(as.data.frame(q1), ...)

Here's a reproducible example (i.e. you could copy-paste it into your R session to reproduce the bug + fix).

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.

Now let's try fix it by making sure that write.xlsx gets a data.frame, not a tbl_df!

write.xlsx(as.data.frame(iris2), file='test.xlsx', sheetName="Sheet1", col.names=TRUE, row.names=FALSE, append=TRUE)
# huzzah!

I find this happens when grouping variables with dplyr. If you end a chain with %>% ungroup () it appears to resolve

It seems that there is a bug with the Date/Time format of the first column (Timestamp). If you convert the first column to character, it should work. So, you may change your first column to

q1[,1] <- as.character(q1[,1])

and try again...

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!