What does |> (pipe greater than) mean in R?
Asked Answered
D

4

53

I have recently come across the code |> in R. It is a vertical line character (pipe) followed by a greater than symbol.

Here is an example:

mtcars |> head()

What is the |> code doing?

Diannadianne answered 28/5, 2021 at 19:35 Comment(0)
D
68

|> is the base R "pipe" operator. It was new in version 4.1.0.

In brief, the pipe operator provides the result of the left hand side (LHS) of the operator as the first argument of the right hand side (RHS).

Consider the following:

1:3 |> sum()
#[1] 6

Here, the vector of numbers 1 through 3 is provided as the first argument of the sum function.

The left hand side result always becomes the first argument of the right hand side call. Consider:

args(sum)
#function (..., na.rm = FALSE) 

c(1:3, NA_real_) |> sum(na.rm = TRUE)
#[1] 6

The emphasis on call is important because you can redirect the LHS to other arguments as long as the first argument is named. Consider:

args(rnorm)
#function (n, mean = 0, sd = 1) 
100 |> rnorm(n = 5)
#[1]  99.94718  99.93527  97.46838  97.38352 100.56502

args(sum)
#function (..., na.rm = FALSE) 
sum(na.rm = TRUE, ... = c(1:2,NA_real_))
#[1] 3
TRUE |> sum(... = c(1:2,NA_real_))
#[1] NA

One benefit of using the |> operator is that it can make code more easy to follow logically compared to nested function calls:

split(x = iris[-5], f = iris$Species) |>
  lapply(min) |>
  do.call(what = rbind) 
#           [,1]
#setosa      0.1
#versicolor  1.0
#virginica   1.4

#Compared to:
do.call(rbind,lapply(split(iris[-5],iris$Species),min))

This functionality is similar to the magrittr::%>% operator (also implemented in dplyr).

However, unlike %>%, there is no current way to pipe the LHS into the right hand side multiple times or into arbitrary positions. Magrittr uses the . placeholder for the LHS and {} to place it arbitrarily.

library(magrittr)
iris[iris$Sepal.Length > 7,] %>% subset(.$Species=="virginica")

TRUE %>% {sum(c(1:2,NA_real_),na.rm = .)}
[1] 3

Additionally, unlike the base R |>, the %>% operator can pipe into function calls without ():

1:3 |> sum
#Error: The pipe operator requires a function call as RHS

1:3 %>% sum
#[1] 6
Diannadianne answered 28/5, 2021 at 19:35 Comment(6)
It's also worth noting that this throws an error: 1:3 |> sum whereas this does not 1:3 %>% sum.Connive
You could use anonymous function TRUE |> {\(x) sum(c(1:2,NA_real_), na.rm = x)}()# [1] 3Infrared
@G.Grothendieck not without the parentheses.Connive
Obviously. That was the point I was making.Entrance
I think it's important to mention that 1:3 |> sum() is parsed as sum(1:3), this is a big difference with {magrittr}Novelize
R 4.2 introduced the placeholder _ to put the LHS into any named RHS parameter, albeit only once. For a lack of a better example, this works: 'world' |> gsub(pattern='there', replace=_, 'hello there'), but this doesn't: 'world' |> gsub(pattern=_, replace=_, 'hello there'). I imagine this will be worked on in future R releases.Brake
M
17

To see how the piped code gets parsed we may use quote().

Examples:

quote(1:3 |> sum())
# sum(1:3)

quote(100 |> rnorm(n = 5))
# rnorm(100, n = 5)

quote(split(x = iris[-5], f = iris$Species) |>
        lapply(min) |>
        do.call(what = rbind))
# do.call(lapply(split(x = iris[-5], f = iris$Species), min), what = rbind)
Merritt answered 28/3, 2022 at 16:55 Comment(0)
S
13

Since I also recently stumbled upon this pipe in a peer's code I looked into the topic and found out that as of R 4.2 you can also pipe the LHS into the right-hand side at arbitrary positions (however, not multiple times, only once) and with a different syntax:

As of R 4.2: you can use |> in combination with _

This means, base R can do some of what magittr does, too:

# magittr
library(magrittr)
TRUE %>% sum(c(1:2, NA_real_), na.rm = .)

# R 4.2 onwards
TRUE |> sum(c(1:2, NA_real_), na.rm = _)

As of R 4.1 you can use => in combination with a variable that you pipe into |> a => f(..., x = a, ...)

# R 4.1 onwards
# you have to set the '_R_USE_PIPEBIND_' envvar to a true value to enable =>
Sys.setenv("_R_USE_PIPEBIND_"=TRUE)
TRUE |> a => sum(c(1:2, NA_real_), na.rm = a)
Seriema answered 23/11, 2022 at 10:42 Comment(2)
TRUE |> {sum(c(1:2,NA_real_),na.rm = _)} Error: function '{' not supported in RHS call of a pipeMetempsychosis
@DongdongKong I cared for it, I think the curly braces were overkill.Merritt
L
-1

|> This is a simple native forward pipe syntax in r. It inserts the left-hand side as the first argument in the right-hand side call. For more clarity visit this link

Loss answered 19/7, 2024 at 19:39 Comment(1)
Please check that your answer has not already been provided before posting.Recaption

© 2022 - 2025 — McMap. All rights reserved.