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.


Photo Credit: N.N.: pipes-1008898_1920.jpg © CC0

About This Author

Matthias studied Environmental Information Management at the University of Natural Resources and Life Sciences Vienna and holds a PhD in environmental statistics. The focus of his thesis was on the statistical modelling of rare (extreme) events as a basis for vulnerability assessment of critical infrastructure. He is working at the Austrian national weather and geophysical service (ZAMG) and at the Institute of Mountain Risk Engineering at BOKU University. He currently focuses the (statistical) assessment of adverse weather events and natural hazards, and disaster risk reduction. His main interests are statistical modelling of environmental phenomena as well as open source tools for data science, geoinformation and remote sensing.

Post A Reply