Recently I have found the %$%
pipe operator, but I am missing the point regarding its difference with %>%
and if it could completely replace it.
Motivation to use %$%
- The operator
%$%
could replace%>%
in many cases:
mtcars %>% summary()
mtcars %$% summary(.)
mtcars %>% head(10)
mtcars %$% head(.,10)
- Apparently,
%$%
is more usable than%>%
:
mtcars %>% plot(.$hp, .$mpg) # Does not work
mtcars %$% plot(hp, mpg) # Works
- Implicitly fills the built-in data argument:
mtcars %>% lm(mpg ~ hp, data = .)
mtcars %$% lm(mpg ~ hp)
- Since
%
and$
are next to each other in the keyboard, inserting%$%
is more convenient than inserting%>%
.
Documentation
We find the following information in their respective help pages.
(?magrittr::`%>%`
):
Description:
Pipe an object forward into a function or call expression.
Usage:
lhs %>% rhs
(?magrittr::`%$%`
):
Description:
Expose the names in ‘lhs’ to the ‘rhs’ expression. This is useful
when functions do not have a built-in data argument.
Usage:
lhs %$% rhs
I was not able to understand the difference between the two pipe operators. Which is the difference between piping an object and exposing a name? But, in the rhs of %$%
, we are able to get the piped object with the .
, right?
Should I start using %$%
instead of %>%
? Which problems could I face doing so?