R >4.1 syntax: Error: function 'function' not supported in RHS call of a pipe

R >4.1 syntax: Error: function 'function' not supported in RHS call of a pipe

R 4.1.0 famously introduced |>(“基本管道”)运算符和类似于 Haskell 的 lambda 函数语法。

我认为可以像这样将两者结合起来:

c(1, 2, 3) |> \(x) 2 * x

这对我来说失败了:

Error: function 'function' not supported in RHS call of a pipe

因此我假设这不是有效的语法?这有效:

c(1, 2, 3) |> (\(x) 2 * x)()

是否有更优雅的方式来链接管道和新的 lambda 函数?

这是本地管道的局限性。你只需要在函数名后面加上(),这与magrittr不同。

# native pipe
foo |> bar()
# magrittr pipe
foo %>% bar

也就是说,\(x) 2*x相当于旧的匿名函数语法function (x) 2*x,但类似于命名函数, 在native pipe的RHS上使用时,必须包含().

我认为最优雅的方式是使用花括号:

c(1, 2, 3) |> {\(x) 2 * x}()