203

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)
zx8754
  • 52,746
  • 12
  • 114
  • 209
ben_says
  • 2,433
  • 4
  • 17
  • 18
  • 3
    See [here](http://cran.r-project.org/web/packages/magrittr/index.html), which is being used heavily in **dplyr**. – joran Jul 02 '14 at 16:18
  • 14
    It has no meaning in R. The magrittr package (used by dplyr) defines a meaning for it: http://cran.r-project.org/package=magrittr – G. Grothendieck Jul 02 '14 at 16:20
  • Take a look on this: http://stackoverflow.com/questions/27125672/what-does-function-mean-in-r?noredirect=1#comment53064401_27125672 – alfakini Sep 17 '15 at 14:06
  • 2
    Possible duplicate of [What does %>% function mean in R?](https://stackoverflow.com/q/27125672/1255289) – miken32 Feb 23 '18 at 16:40

1 Answers1

376

The infix operator %>% is not part of base R, but is in fact defined by the package magrittr (CRAN) and is heavily used by dplyr (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 to head():

library(magrittr)
iris %>% head()
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
2          4.9         3.0          1.4         0.2  setosa
3          4.7         3.2          1.3         0.2  setosa
4          4.6         3.1          1.5         0.2  setosa
5          5.0         3.6          1.4         0.2  setosa
6          5.4         3.9          1.7         0.4  setosa

Thus, iris %>% head() is equivalent to head(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 to head(), then the result of that is passed to summary().

iris %>% head() %>% summary()

Thus iris %>% head() %>% summary() is equivalent to summary(head(iris)). Some people prefer chaining to nesting because the functions applied can be read from left to right rather than from inside out.

MBT
  • 21,733
  • 19
  • 84
  • 102
Andrie
  • 176,377
  • 47
  • 447
  • 496
  • Can we override this %>% operator with unix | in R? – Sujoy Sep 28 '19 at 23:10
  • 2
    I imagine you could, but it would probably interfere with the use of the | operator for "or", which is sometimes needed. – wonder Oct 07 '19 at 17:51
  • Perhaps a silly question but I'm curious how does `%>%` reference The Treachery of Images asides from being called literally a pipe? The pipe operator is well known in bash scripting so I thought that was what it was referencing that? – Null Salad Oct 22 '21 at 22:53
  • @NullSalad The Treachery of Images is simply quoted in the package description, apparently as mere wordplay, see here: https://cran.r-project.org/web/packages/magrittr/index.html – Dmitry Orlov Nov 21 '21 at 17:07
  • Nicely explained! I still want to know, though: when is it best practice to use %>% instead of just nesting functions? Is there some convention for when each syntax ought to be used? – Duncan MacIntyre May 18 '22 at 07:26