FantasyGutsy
FantasyGutsy

Reputation: 91

Nested function is a missing argument - magrittr error

This may go against the philosophy of magrittr (ref1, ref2)

Still, magrittr throws an error in the simplest of nested functions.

x <- c(3, 1,2, 3) %>%
  sort(unique())
## Error in is.factor(x) : argument "x" is missing, with no default
x <- c(3, 1,2, 3) %>%
  sort(unique(.))
## Error in sort(., unique(.)) : 
##   'decreasing' must be a length-1 logical vector.
## Did you intend to set 'partial'?

Can any explain this behaviour or how to avoid it?

Upvotes: 1

Views: 125

Answers (1)

Dunois
Dunois

Reputation: 1843

I think I can provide a partial answer (concerning how to avoid this behavior).

Just enclose the nested function call in braces like so:

c(3, 1, 2, 3) %>% {sort(unique(.), decreasing = FALSE)}
# [1] 1 2 3

As to why this works, that's explained in ?magrittr::%>%\.

Upvotes: 1

Related Questions