Magrittr 转发管道无法将值转发到 openXL::addWorksheet - “错误 ...:第一个参数必须是工作簿" [英] Magrittr forward pipe fails to forward values into openXL::addWorksheet - "Error ...: First argument must be a Workbook"

查看:20
本文介绍了Magrittr 转发管道无法将值转发到 openXL::addWorksheet - “错误 ...:第一个参数必须是工作簿"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

magrittr 似乎无法将workbook"类对象从包 openxlsx 传送到 addWorkbook 函数中.强>

(别介意为什么我需要使用 excel...哎哟 yuk)

例如,将 InsectSprays 数据集以基本"语法写入 excel 文件:

library("openxlsx")昆虫.wb <- createWorkbook()addWorksheet(wb = 昆虫.wb,sheetName = "昆虫喷雾")writeData(wb = 昆虫.wb,sheet = "昆虫喷雾",x= 昆虫喷雾)openXL(昆虫.wb)

打开一个包含数据的临时 excel 文件.

所以magrittr应该

<块引用>

将值通过管道传递到表达式或函数调用中;某物沿着 x %>% f 的行,而不是 f(x)

但是例如

library("openxlsx")图书馆(magrittr")昆虫.wb <- createWorkbook()昆虫.wb %>%addWorksheet(sheetName = "Insect Spray") %>%writeData(sheet = "昆虫喷雾",x= 昆虫喷雾)

退货

<块引用>

writeData(., sheet = "Insect Spray", x = InsectSprays) 中的错误:第一个参数必须是工作簿.

但是 insect.wb 是一个工作簿对象:

 >昆虫.wb <- createWorkbook()>类(昆虫.wb)[1] 《工作簿》attr(,"包")[1] "openxlsx"

这表明问题在于对象没有通过管道输入...

我是在搞乱语法,还是有更有趣的解释来解释为什么会失败?

出于兴趣,前向管道和 writeData 没有问题——如果我们将前向管道的使用转移到 addWorksheet 之后,它也可以正常工作:

insect.wb <- createWorkbook()addWorksheet(wb = 昆虫.wb,sheetName = "昆虫喷雾")昆虫.wb %>%writeData(sheet = "昆虫喷雾",x= 昆虫喷雾)openXL(昆虫.wb)

解决方案

在您的命令链中,writeData 将 insect.wb %>% addWorksheet(sheetName = "Insect Spray") 的输出作为第一个输入

addWorksheet 直接修改对象,并且不返回对象,因此无论您传递给 writeData 什么都不是工作表(根据您的评论,它可能是添加后的工作表数).

您可以使用运算符 %T>% 并编写

insect.wb %T>%addWorksheet(sheetName = "Insect Spray") %>%writeData(sheet = "Insect Spray", x= InsectSprays)

%T>% 开始一个新链,因为不返回 rhs 的输出,但返回 lhs,因此 writeData 将它带到 %T>% 离开了,除了 insect.wb 现在已被 addWorksheet 修改.

magrittr appears to be failing to pipe 'workbook' class objects into the addWorkbook function from the package openxlsx.

(Never mind why I need to use excel...eugh yuk)

For example, to write the InsectSprays dataset to an excel file in 'base' syntax:

library("openxlsx")
insect.wb <- createWorkbook()
addWorksheet(wb = insect.wb,
             sheetName = "Insect Spray")
writeData(wb = insect.wb,
          sheet = "Insect Spray",
          x= InsectSprays)

openXL(insect.wb)

Opens a temp excel file with the data.

So magrittr should

pipe a value forward into an expression or function call; something along the lines of x %>% f, rather than f(x)

But e.g.

library("openxlsx")
library("magrittr")
insect.wb <- createWorkbook()
insect.wb %>%
  addWorksheet(sheetName = "Insect Spray") %>%
  writeData(sheet = "Insect Spray",
            x= InsectSprays)

Returns

Error in writeData(., sheet = "Insect Spray", x = InsectSprays) :
First argument must be a Workbook.

But insect.wb is a workbook object:

 > insect.wb <- createWorkbook()
 > class(insect.wb)

[1] "Workbook"
attr(,"package")
[1] "openxlsx"

Which suggests that the issue is that the object isn't getting piped in...

Am I just buggering up the syntax, or is there a more interesting explanation for why this is failing?

For interest, there is no issue with forward pipe and writeData –- if we shift the use of forward-pipes until after addWorksheet, it also works fine:

insect.wb <- createWorkbook() 
addWorksheet(wb = insect.wb,
             sheetName = "Insect Spray")

insect.wb %>%
  writeData(sheet = "Insect Spray",
            x= InsectSprays)

openXL(insect.wb)

解决方案

In your command chain, writeData takes as a first input the output of insect.wb %>% addWorksheet(sheetName = "Insect Spray")

addWorksheet modifies directly the object, and doesn't return the object, so whatever you're passing to writeData is NOT a worksheet (from your comment, it's probably the number of sheets after the add).

You may use the operator %T>% and write

insect.wb %T>%
  addWorksheet(sheetName = "Insect Spray") %>%
  writeData(sheet = "Insect Spray", x= InsectSprays)

%T>%starts a new chain in the sense that the ouput of the rhs is not returned, but the lhs is returned, so then writeData takes it where %T>% left, except that insect.wb has now been modified by addWorksheet.

这篇关于Magrittr 转发管道无法将值转发到 openXL::addWorksheet - “错误 ...:第一个参数必须是工作簿"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆