Pipe Operator (%>% and |>) (R)
The pipe operator passes the result on its left into the next function on its right, letting you chain operations left-to-right. R has the magrittr pipe %>% and the native base-R pipe |>.
R
The pipe turns nested calls into readable sequences: instead of summarise(group_by(filter(df, x > 0), g), m = mean(x)), you write df %>% filter(x > 0) %>% group_by(g) %>% summarise(m = mean(x)). The value flows top-to-bottom and left-to-right, matching how you think about a data pipeline.
There are two pipes. %>% comes from the magrittr package (re-exported by dplyr and the tidyverse) and is very flexible, including the . placeholder for the piped value. |> is the native pipe built into R 4.1+, with no package needed; it's slightly more limited but is now the recommended default for new code.
Pipes are central to idiomatic tidyverse R, making dplyr and ggplot2 workflows clean. By default the left value goes into the first argument of the right-hand function.
Syntax
x %>% f() %>% g() # magrittr
x |> f() |> g() # native (R 4.1+)Examples
R
result <- c(1, 4, 9, 16) |> sqrt() |> sum()
print(result)Output:
[1] 10