【问题标题】:Error in printing data.frame in excel using XLSX package in R使用 R 中的 XLSX 包在 excel 中打印 data.frame 时出错
【发布时间】:2015-12-23 03:12:57
【问题描述】:

数据框是可见的,没有任何错误。但是当使用 XLSX 包的 write.xlsx 函数打印相同的内容时,它会给出错误。

Error in .jcall(cell, "V", "setCellValue", value) : 
  method setCellValue with signature ([D)V not found.

data.frame 的 dput 如下所示:

Timestamp         qs          pqs        logqs         es         p_imp      dep    r_dep       agg_rtn
               (time)      (dbl)        (dbl)        (dbl)      (dbl)         (dbl)    (dbl)    (dbl)         (dbl)
1 2015-05-04 09:29:59 0.05788732 0.0007478696 0.0007478545 0.09633803 -0.0446830986 3533.518 274079.9 -0.0006432937
2 2015-05-04 10:00:00 0.04948394 0.0006362707 0.0006362707 0.07586009  0.0088016055 2416.431 187953.1  0.0000000000
3 2015-05-04 10:30:00 0.05554795 0.0007142532 0.0007142532 0.06417808 -0.0002739726 3245.574 252422.0  0.0000000000
4 2015-05-04 10:59:59 0.04863014 0.0006194244 0.0006194244 0.08434442  0.0024951076 3563.401 279503.9  0.0000000000
5 2015-05-04 11:30:00 0.05761986 0.0007319037 0.0007319037 0.07851027  0.0154965753 2010.943 158429.1 -0.0006339144
6 2015-05-04 12:00:00 0.04957627 0.0006285051 0.0006285051 0.07025424  0.0070762712 1819.908 143546.0  0.0000000000
Variables not shown: vol_30_sum (dbl), vol_30_mean (dbl), p_return_sqr (dbl), p_return_mean (dbl), Lim_or_out (dbl),
  closing_price (dbl), closing_vol (dbl)

请帮助解决此错误。

【问题讨论】:

  • “当使用 write.xlsx 函数打印相同内容时”是什么意思?你在做print(write.xlsx(...))吗?还是只是在您致电write.xlsx(...) 时出现错误?你能提供一个可重现的例子吗?
  • 输出打印使用:write.xlsx(q1, file = paste0(File Address","_6",".xlsx"),sheetName="Sheet1",col.names=TRUE, row .names=FALSE, append=TRUE)
  • 你能提供一个reproducible example吗?此外,您的“data.frame 的输入”看起来不像dput(my.dataframe) 会产生的东西,您能否查看class(my.dataframe) 并确认(唯一)类是“data.frame”?
  • class(q1) [1] "tbl_df" "tbl" "data.frame"

标签: r excel xlsx


【解决方案1】:

仍然没有可重现的示例,但从您的 class(q1) 看来,q1tbl_dfdplyr 包产生的那种数据帧),而 write.xlsx 需要 data.frame

尝试给write.xlsx 一个简单的data.frame,正如它所期望的那样。例如

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

这是 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!

【讨论】:

    【解决方案2】:

    我发现使用 dplyr 对变量进行分组时会发生这种情况。如果你用 %>% ungroup () 结束一个链,它似乎可以解决

    【讨论】:

    • 这成功了!以 ungroup() 结尾使我能够保持我的 dplyr 分析链完整,并且仍然使用 write.xlsx() 导出
    【解决方案3】:

    第一列(时间戳)的日期/时间格式似乎存在错误。如果将第一列转换为字符,它应该可以工作。 因此,您可以将第一列更改为

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

    然后再试一次...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-19
      • 2014-01-31
      • 1970-01-01
      • 2016-03-17
      • 2014-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多