This seems cleaner to me because it does not need to use %T>% (which IMHO makes it easy to reinstall and read the channel) and {} around the expression to avoid passing the object there. I am not sure how much harm there is in transferring an object and ignoring it.
I never used the %T>% tag, where I also did not want to print or draw. And I never wanted to print / build an object that is being broadcast (usually this is a large data set). Therefore, I never use %T>% .
library("ggplot2") library("dplyr") pap = function(pass, to_print = NULL, side_effect = NULL) { if( !is.null(to_print)) { if (is.function(to_print)) { print(to_print(pass)) } else { print(to_print) } } side_effect invisible(pass) } mtcars %>% pap(summary) %>% pap(side_effect = plot(.)) %>% pap(qplot(mpg, cyl, data = .)) %>% summarise(mean(mpg))
I usually don’t use build as a side effect in my pipes, so the solution above works best for me (for the “side effect”, an “extra setup” is required). I would like to be able to unambiguously sort these scenarios (for example, a graph against qplot), but I did not find a reliable way.
Ruben
source share