The magrittr package offers a new operator that can help improve readability of your code, and make it easier to update and modify data wrangling code. The %>% operator has been adopted into dplyr and many of Hadley Wickham’s packages are written to be pipe-friendly.
The Problem
R code can get hard to read
A (Possible) Solution - the pipe %>%
Similar to Unix pipe |
Code can be written in the order of execution, left to right
%>% will “pipe” information from one statement to the next
x %>% f is equivalent to f(x)
x %>% f(y) is equivalent to f(x,y)
x %>% f %>% g %>% h is equivalent to h(g(f(x)))
{magittr} provides 4 special operators
%>% - pipe operators
%T>% - tee operator
%$% - exposition operator
%<>% - compound assignment pipe operator
What %>% is doing
The %>% is taking the output of the left-hand side and using that for
the first argument of the right-hand side, or where it finds a .