While debugging dplyr chains in a function, I often want to be able to print intermediate changes to a data.frame to the console. How would I do this?
For example:
data(cars)
cars %>% group_by(speed) %>%
summarize(dist_mean = mean(dist)) %>%
[insert printing function] %>%
group_by(speed > 10) %>%
summarize(dist_mean = mean(dist_mean)) %>%
[insert printing function]
Is there something I can do to replace [insert printing function]
to print the current state of the dataframe to the console, while still continuing the chain uninterrupted?
CodePudding user response:
Try the T-pipe: %T>%
.
From documentation:
Pipe a value forward into a function- or call expression and return the original value instead of the result. This is useful when an expression is used for its side-effect, say plotting or printing.
There is a good example of it here along with the other two less common pipe commands.
Inserting back into your original context:
data(cars)
cars %>% group_by(speed) %>%
summarize(dist_mean = mean(dist)) %T>%
print() %>%
group_by(speed > 10) %>%
summarize(dist_mean = mean(dist_mean)) %T>%
print()
Edit: As per @Ritchie Sacramento's comment the pipes are part of the magrittr package. They also appear to be re-exported by dplyr. If they don't work after calling library(dplyr)
then you will need to call library(magrittr)
to access them.