Posted on Categories Programming, StatisticsTags , , , ,

What is magrittr’s future in the tidyverse?

For many R users the magrittr pipe is a popular way to arrange computation and famously part of the tidyverse.


The tidyverse itself is a rapidly evolving centrally controlled package collection. The tidyverse authors publicly appear to be interested in re-basing the tidyverse in terms of their new rlang/tidyeval package. So it is natural to wonder: what is the future of magrittr (a pre-rlang/tidyeval package) in the tidyverse?

For instance: here is a draft of rlang/tidyeval based pipe (from one of the primary rlang/tidyeval authors). This pipe even fixes dplyr issue 2726:

# do NOT perform the source step directly!
# Instead, save and inspect the source code from:
# Output printed is the result of an example in the gist.
#> List of 1
#>  $ :List of 1
#>   ..$ :List of 2
#>   .. ..$ : chr "foo"
#>   .. ..$ : chr "foo"

# previously throwing example from dplyr issue 2726
(function(x) mtcars %>% dplyr::select(!!enquo(x)))(disp) %>%
#>                   disp
#> Mazda RX4          160
#> Mazda RX4 Wag      160
#> Datsun 710         108
#> Hornet 4 Drive     258
#> Hornet Sportabout  360
#> Valiant            225

4 thoughts on “What is magrittr’s future in the tidyverse?”

  1. lionel’s code makes my head spin. It would be amazing to have some kind of deep dive blog post showing how you even begin to write something like this.

    1. Actually Lionel Henry’s code is sequenced in good “form follows function style” (once you ignore the preparation steps). The difficult bit is only somebody very versed in the internals of rlang/tidyeval can work out what the preparation steps do and which ones would be needed.

      The magrittr code itself is incredibly compact for what it does, as it uses the powerful idea of traversing the parse tree and hanging different effects as it visits different bits of the tree. But that makes it a bit hard to immediately figure out how things interact.

      My code is simple “does what it says on the label” code, but that isn’t always a bad thing. One has to know some R arcana (i.e., that substitute() captures unevaluated arguments), but that is standard for R if you try to directly manipulate calculations.

Leave a Reply