magrittr 1


F# made pipes popular among data scientists, and the magrittr package brought pipes to R. For example, with magrittr, you could write:

instead of

For example:


or even (to take it to the extreme):

This doesn’t look like a revolution, but in practice it makes things a lot easier, as the well-known example from Hadley Wickham shows: In traditional R, a data manipulation action might look like this:

With magrittr, this becomes:

The package also defines other operators, e.g. for assiging back to the original variable after the data manipulation. Here, however, I personally much prefer the classic R notation, e.g. along the lines of

The key is the -> . The reason I like this a lot is that it keeps the flow of pipes: Take something, do something with it, and at the end assign it to a variable. The magrittr alternative would be the %<>% operator, which in my opinion is much less readable:

By the way, the art-lovers will have guessed where the name magrittr comes from: French 20th century painter René Magritte painted the famous work “Ceci n’est pas une pipe” (this is not a pipe). We appreciate that Stefan Milton Bache, the author of magrittr, brought classic art to R, and forgive him the little typo in the vignette (pipe is féminine, i.e. une pipe, and not un pipe) 😉


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

One thought on “magrittr

  • gluc Post author

    Stefan replied, and explained: It’s not a typo (or one might say it’s a deliberate typo): a computer pipe is masculine 😉