R Pipeline operators

R
magrittr and own definitions
Author

Zhenglei Gao

Published

June 12, 2025

Warning

To be written.

What I often use

%||%

Quoted from R base control Help documentation: > The null coalescing operator %||% is a simple 1-line function: x %||% y is an idiomatic way to call

if (is.null(x)) y else x
                         # or equivalently, of course,
if(!is.null(x)) x else y 

Inspired by Ruby, it was first proposed by Hadley Wickham.

res <- {}
res %||% "alternative result"
[1] "alternative result"
library(magrittr)

Forward-pipe operator

R now has |> in base.

rnorm(5000) %>%
  abs %>% `*` (100)  %>%
  matrix(ncol=10)  %>%
  rowMeans %>% round %>% 
  `%%`(9) %>% hist

Tee operator (left) %T>%

rnorm(5000) %>%
  abs %>% `*` (100)  %>%
  matrix(ncol=10)  %>%
  rowMeans %>% round %>% 
  `%%`(9) %T>% hist %>% sum

[1] 2077

Exposition pipe-operator

Compound assignment pipe-operator