Having several long and redundant chains in R ‘s magittr code, I have now figured out how to pipe into named and unnamed functions
f1 <- function(x) { x %>% count() %>% print() } f2 <- function(x) { x %>% tibble() %>% print() }
So we have now two named functions with code blocks that can be inserted in an unnamed function whenever needed
iris %>% # do any select, mutate .... # before running both functions (function(x) { x %>% select(Species) %>% f1() %>% print() x %>% f2() %>% print() })
We can make a variable global from inside a function using <<- assignment but I have not found a way to continue the dplyr chain with any returned variable. So we are trapped inside the function
iris %>% (function(x) { result <<- x %>% head(1) })