Pipes in R {magrittr}

Dear all,

back in 2014, Hadley Wickham’s dplyr tutorial at useR!2014 drew a lot of attention to the %>% (pipe) operator from the magrittr package. While the pipe operator is an essential part of the tidyverse workflow, and is thus well-known to users of packages belonging to the umbrella of tidyverse (e.g. dplyr, tidyr or purrr), the pipe operator might still be new to some of you.

While I initially intended to write a short introduction to pipes in R a couple of weeks ago, things changed when I came across a recent tutorial by Karlijn Willems on the DataCamp blog. My subsequent search quickly revealed that another post on the pipe operator would be somehow superfluous, given the large number of excellent tutorials on the topic. Thus, instead of replicating information already available in an appealing way elsewhere, I’d like to rather provide a couple of links to interesting sources containing where you can further information on the topic.

There are several nice vignettes, tutorials and blog posts on the pipe operator. In addition, there’s a dedicated section in Hadley Wickham’s excellent book R for Data Science:

I do have some additional remarks, though:

  • In the context of geoinformatics and spatial data management, it is worth knowing that the sf package is designed to support dplyr-style verbs and the pipe-operator.
  • The magrittr package also contains the%<>% operator (called the magritts compound assignment pipe-operator), which pipes an object forward into a function or call expression and update the initial object with the resulting value. For instance, instead of x <- as_data_frame(x) you can simply use x %<>% as_data_frame.


About This Author

<p>Matthias studied Environmental and Bio-Resources Management with a specialization in Environmental Information Management at the University of Natural Resources and Life Sciences (Vienna). He is currently a PhD student working at the Austrian Institute of Technology. Having written his master’s thesis about extreme weather risk identification for the Austrian road network, he currently focuses on modeling of adverse weather events as a basis for risk assessment of road infrastructure networks.</p>

Post A Reply