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:
- Hadley Wickham: Chapter 18: Pipes in R for Data Science
- Hadley Wickham: Section ‘Piping’ of the
- Stefan Milton Bache / Hadley Wickham:
- Stefan Milton Bache / Tal Galili: Simpler R coding with pipes > the present and future of the magrittr package
- David Smith: magrittr: simplifying R code with pipes
- Francis Smart: More Readable Code with Pipes in R
- Martin Johnsson: More fun with %.% and %>%
I do have some additional remarks, though:
- In the context of geoinformatics and spatial data management, it is worth knowing that the
sfpackage is designed to support dplyr-style verbs and the pipe-operator.
magrittrpackage 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.