r - Stepping through a pipeline with intermediate results -


is there way output result of pipeline @ each step without doing manually? (eg. without selecting , running selected chunks)

i find myself running pipeline line-by-line remember doing or when developing analysis.

for example:

library(dplyr)  mtcars %>%    group_by(cyl) %>%    sample_frac(0.1) %>%    summarise(res = mean(mpg)) # source: local data frame [3 x 2] #  # cyl  res # 1   4 33.9 # 2   6 18.1 # 3   8 18.7 

i'd select , run:

mtcars %>% group_by(cyl) 

and then...

mtcars %>% group_by(cyl) %>% sample_frac(0.1) 

and on...

but selecting , cmd/ctrl+enter in rstudio leaves more efficient method desired.

can done in code?

is there function takes pipeline , runs/digests line line showing output @ each step in console , continue pressing enter in demos(...) or examples(...) of package guides

it easy magrittr function chain. example define function my_chain with:

foo <- function(x) x + 1 bar <- function(x) x + 1 baz <- function(x) x + 1 my_chain <- . %>% foo %>% bar %>% baz 

and final result of chain as:

     > my_chain(0)     [1] 3 

you can function list functions(my_chain) , define "stepper" function this:

stepper <- function(fun_chain, x, fun = print) {   f_list <- functions(fun_chain)   for(i in seq_along(f_list)) {     x <- f_list[[i]](x)     fun(x)   }   invisible(x) } 

and run chain interposed print function:

stepper(my_chain, 0, print)  # [1] 1 # [1] 2 # [1] 3 

or waiting user input:

stepper(my_chain, 0, function(x) {print(x); readline()}) 

Comments