Piping

Etienne Bacher
Will Gearty

2026-11-16

Objectives


Learn how to use the pipe operator (|>) to write more readable code.

Without the pipe

Example


We want to process mtcars:

  • keep the first 10 rows
  • keep observations where cyl >= 6
  • sort the data by am


Two common approaches: intermediate objects or nested calls.

Intermediate objects


Overwrite the same object at each step:

res <- head(mtcars, 10)
res <- subset(res, cyl >= 6)
res <- sort_by(res, ~ am)


  • Renaming res means renaming every occurrence.
  • If one step fails, we must rerun the whole block.

Intermediate objects


Use temporary names:

tmp1 <- head(mtcars, 10)
tmp2 <- subset(tmp1, cyl >= 6)
res <- sort_by(tmp2, ~ am)


  • Pollutes the environment with temporary objects.
  • Adding a step means renaming many objects.

Nested calls


Run all calls at once:

res <- sort_by(
  subset(
    head(mtcars, 10),
    cyl >= 6
  ),
  ~ am
)


No intermediate objects, but we must read from the inside out.

Introducing the pipe

Using |>


The pipe passes the output of the left-hand side to the next call:

res <- head(mtcars, 10) |>
  subset(cyl >= 6) |>
  sort_by(~ am)

Read it as “and then”:

  • keep the first 10 rows, and then
  • filter, and then
  • sort.

Using |>


  • No intermediate objects.
  • Operations are read in the order they run.


|> is available since R 4.1 (2021).

Assigning the output


We can also assign at the end of the chain with ->:

head(mtcars, 10) |>
  subset(cyl >= 6) |>
  sort_by(~ am) -> res

Using _

Piping to non-first arguments


grepl() takes the vector to check in second position:

let <- letters[1:6]
grepl("a|e", let)
[1]  TRUE FALSE FALSE FALSE  TRUE FALSE

Piping without care gives incorrect code:

let |> grepl("a|e")
Warning in grepl(let, "a|e"): argument 'pattern' has length > 1 and only the
first element will be used
[1] TRUE

The placeholder _


_ tells the piped data where to go:

let |> grepl("a|e", x = _)
[1]  TRUE FALSE FALSE FALSE  TRUE FALSE


It also works for extraction:

mtcars |> _$drat |> mean()
[1] 3.596563

Restrictions of _

Available since R 4.2 (2022).


Must be used with a named argument:

let |> grepl("a|e", _)
Error in grepl("a|e", "_"): pipe placeholder can only be used as a named argument (<input>:1:8)


Only one _ per function call:

mtcars$drat |> cor(x = _, y = _)
Error in cor(x = "_", y = "_"): pipe placeholder may only appear once (<input>:1:16)

Anonymous functions

Named functions


Named functions are defined first and can be used multiple times:


my_function <- function(x) {
  (x - mean(x)) / sd(x)
}

lapply(my_list, my_function)

my_function(1:3)

Anonymous functions


Anonymous functions are created on the fly and can only be used once:

lapply(my_list, function(x) {
  (x - mean(x)) / sd(x)
})


In a pipe, wrap it in parentheses and call it with ():

mtcars |>
  subset(cyl == 4) |>
  (function(d) lm(mpg ~ disp, data = d))()

What about %>%?

%>% vs |>


  • %>% comes from the magrittr package.
  • It was widely used in the tidyverse.
  • It inspired |> in base R.


For simple cases, both behave the same.


We recommend |> since it is always available, and doesn’t require an extra package.