Implementing magrittr

Using the magrittr pipe in your own package
R
Author

Harvey

Published

July 16, 2021

magrittr is the pipe, commonly used in tidyverse packages, that enables simple sequencing of operators. The simple forward pipe replaces f(x, y) with x %>% f(y). Code becomes much more readable when chaining multiple pipes together.
It’s very simple to use this in any package. the usethis package provides a command usethis::use_pipe() which takes care of everything for you. Once run, the magrittr pipe operator will be exported and available for users.

add_number <- function(x, y) {
  return(x + y)
}

3 %>% 
  add_number(5) %>% 
  add_number(2)
[1] 10