I am following this example, the server.R, file is here.
I plan to do a similar filter, but am lost as to what %>%
does.
# Apply filters
m <- all_movies %>%
filter(
Reviews >= reviews,
Oscars >= oscars,
Year >= minyear,
Year <= maxyear,
BoxOffice >= minboxoffice,
BoxOffice <= maxboxoffice
) %>%
arrange(Oscars)
The infix operator
%>%
is not part of base R, but is in fact defined by the packagemagrittr
(CRAN) and is heavily used bydplyr
(CRAN).It works like a pipe, hence the reference to Magritte's famous painting The Treachery of Images.
What the function does is to pass the left hand side of the operator to the first argument of the right hand side of the operator. In the following example, the data frame
iris
gets passed tohead()
:Thus,
iris %>% head()
is equivalent tohead(iris)
.Often,
%>%
is called multiple times to "chain" functions together, which accomplishes the same result as nesting. For example in the chain below,iris
is passed tohead()
, then the result of that is passed tosummary()
.Thus
iris %>% head() %>% summary()
is equivalent tosummary(head(iris))
. Some people prefer chaining to nesting because the functions applied can be read from left to right rather than from inside out.