add_pipe: build a new pipe in one command

Description Usage Arguments Value Examples

View source: R/04_add_pipe.R

Description

The argument 'new_body' will often be a 'substitute' call, the variable '.' contains the output of the previous operation while the variable body contains the call that would be executed by a regular ' it to get the new behavior.

Usage

1
add_pipe(new_pipe, new_body)

Arguments

new_pipe

A name

new_body

A call

Value

the operator is created and *magrittr*'s namespace is modified to make the new operator compatible. NULL is returned.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# create a pipe that prints the dimensions before executing the relevant step
add_pipe(`%dim1>%`, substitute({print(dim(.)); . <- b; cat("\n"); .}, list(b = body)))
iris %dim1>% head(2)
# create a pipe that prints the dimensions after executing the relevant step
add_pipe(`%dim2>%`, substitute({. <- b; print(dim(.)); cat("\n"); .}, list(b = body)))
iris %dim2>% head(2)
# if we wanted to recreate existing operators
add_pipe(`%T2>%`, call("{", body, quote(.)))
iris %T2>% {message("side effect")} %>% head(2)
add_pipe(`%W2>%`, substitute(
    {options(warn = -1); on.exit(options(warn = w)); b},
    list(w = options()$warn, b = body)))
data.frame(a = c(1,-1)) %W2>% transform(a = sqrt(a))
add_pipe(`%P2>%`, substitute({. <- print(b);cat("\n");.}, list(b = body)))
iris %P2>% head(3) %>% head(2)

moodymudskipper/mmpipe documentation built on May 17, 2019, 10:39 a.m.