使用 ``magrittr::`%>%` `` 时 magrittr 管道出错

Error in magrittr pipe when using ``magrittr::`%>%` ``

无论出于何种原因,我都在使用 magrittr 管道语法,并且在 scope 明确限定对 [=13= 的调用时遇到了一个奇怪的错误].我知道使用下面的语法会破坏管道的用途,但我很好奇为什么会出现错误。

第一次调用 sum 按预期工作并输出 1.

第二次调用导致错误:Error in pipes[[i]] : subscript out of bounds.

library(magrittr)

`%>%`(1,sum())
magrittr::`%>%`(1,sum())

查看管道的源代码,我认为错误的原因与第一行操纵环境有关,但我确定它引入了什么问题。

function (lhs, rhs) {
   parent <- parent.frame()
   env <- new.env(parent = parent)
   chain_parts <- split_chain(match.call(), env = env)

谁能解释这种行为?

管道参数(%>%、%$%、等等...)实际上都是 magrittr 中相同的 pipe() 函数。函数所做的第一件事就是使用内部非导出函数 split_chain.

将调用拆分为其组成部分

split_chain() 获取调用的第一个元素(使用的函数,在本例中为管道运算符之一)并通过另一个名为 is_pipe() 的内部非导出函数运行它看起来像:

function(pipe)
{
  identical(pipe, quote(`%>%`))   ||
  identical(pipe, quote(`%T>%`))  ||
  identical(pipe, quote(`%<>%`))  ||
  identical(pipe, quote(`%$%`))
}

如果返回结果不正确,函数将退出并返回一个列表,该列表缺少管道类型和导致问题的参数的右手边。在范围界定时,la magrittr::'%>%' 调用的第一部分包括显式范围界定,因此它无法通过这些硬编码检查。