1 Pipe: magrittr and native
1.1 Resources
- magrittr package
- Release of R 4.1
- The (updated) history of the pipe operator in R
- Simpler R coding with pipes > the present and future of the magrittr package – 5 August 2014
- Differences between the base R and magrittr pipes
- Isabella Velásquez - Understanding the native R pipe |>
- Tim Tiefenbach - Why continue to use magrittr pipe – 17 October 2022
1.2 Native pipe
- The native pipe was introduced in R 4.1 alongside the use of new anonymous function syntax.
- Pipe the left-hand side into the first argument of the right-hand side. In normal usage, it works almost exactly like the
magrittr
pipe. - In R 4.2 the native pipe got the
_
syntax to insert the left-hand side into a named argument of the right-hand side function. However, this functionality is not as powerful as in%>%
, see Section 1.3.
The tidyverse style guidelines are moving over to the usage of the native pipe. The native pipe is used for all examples in the Second Edition of R for Data Science. Hadley discusses the move to the native pipe in Documentation in the release notes for purrr 1.0.0. There, he notes the advantages of the greater simplicity of the native pipe and the clarity provided by anonymous functions, see Section 1.2.2.
1.2.1 Usage
The pipe operator is implemented as a syntax transformation.
Like the magrittr
pipe, the native pipe places the left-hand side into the first argument of the right hand side.
mtcars |> filter(cyl == 4) |> head()
#> mpg cyl disp hp drat wt qsec vs am gear carb
#> Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
#> Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
#> Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
#> Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
#> Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
#> Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
There are two ways to place the left-hand side elsewhere in the right-hand side function: anonymous functions and _
placeholder
1.2.2 The pipe and the anonymous function
The pipe and the new shorthand syntax for the anonymous function both debuted with R 4.1.
Example of using anonymous function to make code more clear taken from purrr 1.0.0 release. This is part of the move away from formula notation (~
) to anonymous function.
1.2.3 Underscore placeholder
The _
placeholder is similar to magrittr
.
placeholder. However, the _
placeholder can only be used once and must be used with a named argument. See Section 1.3 for examples of these limitations. Nevertheless, the _
placeholder does cover most use cases.
In R 4.3 the _
placeholder can be used with extraction functions $
, or as the head of a chain of extractions [
, [[
, or @
. However, it cannot be used with [[
to start the extraction as in case 4 of the magrittr pipe below.
1.3 The magrittr pipe
Uses of the magrittr
.
notation that are easier or not possible with the native pipe.
- Use of
.
in unnamed arguments
- Use of multiple
.
in right-hand side
- Use of
.
in nested functions
- Use of
.
with infix operators on left- and right-hand side
mtcars %>% `[[`("cyl")
#> [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4