-->

dplyr & r: Anonymous functions myst be parenthesiz

2020-08-13 02:07发布

问题:

I think I've stumbled apon my first error with a spelling mistake.

I am running the following code with R and dplyr.

> foobar = c(1,2,3)
> foobar %>% as.character
[1] "1" "2" "3"

This works fine, now I try to run it through an anonymous function.

> foobar %>% function(x) x * 2 
Error: Anonymous functions myst be parenthesized

Any idea what is happening? (And where I need to ping to get 'myst' corrected to 'must')?

回答1:

The error message is quite informative (even if one word is misspelled). Put parentheses around the anonymous function.

foobar <- 1:3
foobar %>% (function(x) x * 2)
# [1] 2 4 6

For explanation, see the Using %>% with call- or function-producing rhs section in

help("%>%", "magrittr")

It has nothing to do with dplyr. As for the typo in the error message, whenever you find something that might need attention you can contact the package maintainer. Although it seems this has been fixed in the most recent development version of magrittr. An easy way to find the maintainer of a package is to use

maintainer("magrittr")

The result is omitted here because it contains an email address.



标签: r dplyr