pass_through <- function(data, fun) {fun(data); data}
#from Printing intermediate results without breaking pipeline in tidyverse answer
mtcars %>% filter(mpg>15) %>% pass_through(. %>% nrow %>% print)
From the code above, I can print the number of rows of the data after filtering. But I cannot print the difference of number of rows between the original data and the data after filtering.
> mtcars %>% filter(mpg>15) %>% pass_through(. %>% nrow %>% print(.-nrow(mtcars)))
Error in print.default(., . - nrow(mtcars)) : invalid printing digits -6
Question 1: Are there any ways to check the difference without using any extra variables and breaking pipeline?
Question 2: Are there any ways to check the difference between 'n'th pipeline and 'n+1'th pipeline without using any extra variables and breaking pipeline?
For example, by using the code from Gregor Thomas,
mtcars %>%
filter(mpg > 30) %T>% #let this output to be y
{\(x) (nrow(mtcars) - nrow(x)) %>% print}() %>%
filter(cyl > 5) %T>%
{\(x) (nrow(y) - nrow(x)) %>% print}()
#I know it is illegal to write 'y'