I want to have parameterizable parts of pipelines.
This is the code I want to write:
library(magrittr)
d <- data.frame(x=1:5)
add_n <- function(n) . %>% transform(x = x + n)
d %>% add_n(3)
Obviously it doesn't work, because %>%
sets d
as the argument of add_n
.
You can do the following:
add_n <- function(d, n) d %>% transform(x = x + n)
d %>% add_n(3)
# x
# 1 4
# 2 5
# 3 6
# 4 7
# 5 8
%>%
substitutes the first argument of the following function with the LHS, so your function needs a second argument.