dplyr&r:匿名函数myst是括号

can*_*his 11 r dplyr

我想我的第一个错误是因为拼写错误而绊倒了.

我用R和dplyr运行以下代码.

> foobar = c(1,2,3)
> foobar %>% as.character
[1] "1" "2" "3"
Run Code Online (Sandbox Code Playgroud)

这工作正常,现在我尝试通过匿名函数运行它.

> foobar %>% function(x) x * 2 
Error: Anonymous functions myst be parenthesized
Run Code Online (Sandbox Code Playgroud)

知道发生了什么事吗?(我需要ping的地方才能将'myst'更正为'must')?

Ric*_*ven 15

错误消息非常有用(即使一个单词拼写错误).将括号括在匿名函数周围.

foobar <- 1:3
foobar %>% (function(x) x * 2)
# [1] 2 4 6
Run Code Online (Sandbox Code Playgroud)

有关说明,请参阅使用%>%和生成调用或函数生成rhs部分

help("%>%", "magrittr")
Run Code Online (Sandbox Code Playgroud)

它与此无关dplyr.至于错误消息中的拼写错误,每当您找到可能需要注意的内容时,您都可以联系包维护者.虽然它似乎在最新的开发版本中得到修复magrittr.找到包的维护者的简单方法是使用

maintainer("magrittr")
Run Code Online (Sandbox Code Playgroud)

此处省略了结果,因为它包含电子邮件地址.