R Operator - Pipe
A pipe operator passes the output of the last command to the next command, like |
in Shell. A pipe operator simplifies two steps
- Assign the result to a variable
- When using a pipe operator, the output will not be printed to the stdout
- Pass the variable to a command/function
- When piping into a function, its first argument can be omitted
%>%
, a famous pipe operator in R introduced by magrittr
[1] package, is widely used by many R Packages, like dplyr.
mtcars %>%
filter(mpg > 25) %>%
arrange(desc(mpg)) %>%
ggplot(data=.,aes(x=row.names(.), y=mpg)) +
geom_bar(stat="identity")
Behind the scenes, x %>% f(y)
turns into f(x, y)
, and x %>% f(y) %>% g(z)
turns into g(f(x, y), z)
and so on.
The pipe operator enable us to focus on the transformations, not what’s being transformed. It also makes the code easier to read: replace %>%
with "then" when reading code.
In R 4.1, a built-in pipe operator is introduced: |>
. This operator doesn't rely on third-party packages and may be faster. But it lacks two important features that %>%
has
- represent the value being piped in by
.
, as in the above example - when you pipe into a function with no other arguments, you can omit the parentheses
()
Rene Magritte has a famous painting that says "This is not a pipe." ↩︎