Printing intermediate results without breaking pipeline in tidyverse
You could easily write your own function
pass_through <- function(data, fun) {fun(data); data}
And use it like
mtcars %>% pass_through(. %>% ncol %>% print) %>% nrow
Here we use the . %>%
syntax to create an anonymous function. You could also write your own more explicitly with
mtcars %>% pass_through(function(x) print(ncol(x))) %>% nrow
Fleshing out MrFlick's answer for the print
case where it's even simpler:
# Your pipe-print function
pipe_print = function(data) {print(data); data}
# Let's try it in action:
mtcars %>%
mutate(kmpl = 0.425*mpg) %>% # Convert to km-per-liter
pipe_print() %>% # Pipe print! Did it work?
group_by(cyl) %>% # Continue processing...
summarise(mpg = mean(mpg))
You can do on the fly with an anonymous function:
mtcars %>% ( function(x){print(x); return(x)} ) %>% nrow()