renkun-ken / pipeR

Multi-Paradigm Pipeline Implementation
Other
167 stars 39 forks source link

Check how pipeR works with debugging facilities #42

Closed renkun-ken closed 10 years ago

renkun-ken commented 10 years ago

Currently, the side-effect syntax can be used for debugging with browser().

Suppose the original code:

mtcars %>>%
  subset(mpg <= quantile(mpg, 0.95)) %>>%
  lm(formula = mpg ~ .) %>>%
  summary %>>%
  (r.squared)

To debug this code, just insert (~ browser()) to the pipeline after the line one needs to debug. At the browser environment, one only needs to type . to see the input of that line. For example,

mtcars %>>%
  subset(mpg <= quantile(mpg, 0.95)) %>>%
  lm(formula = mpg ~ .) %>>%
  (~ browser()) %>>%
  summary %>>%
  (r.squared)

The debugging looks like

Browse[1]> .

Call:
lm(formula = mpg ~ ., data = .)

Coefficients:
(Intercept)          cyl         disp           hp         drat           wt  
   29.13001     -0.84475      0.01751     -0.02851      0.26103     -3.70579  
       qsec           vs           am         gear         carb  
    0.15113      0.15242     -0.19900      1.05332      0.10792  

Browse[1]> Q
> 

One can also specify parameters of browser() to perform conditional browsing. For example,

mtcars %>>%
  subset(mpg <= quantile(mpg, 0.95)) %>>%
  lm(formula = mpg ~ wt + cyl) %>>%
  (~ browser(expr = summary(.)$r.squared >= 0.9)) %>>%
  plot()

To help identify the browser location, print something ahead.

mtcars %>>%
  subset(mpg <= quantile(mpg, 0.95)) %>>%
  lm(formula = mpg ~ wt + cyl) %>>%
  (~ print("debugging")) %>>%
  (~ browser()) %>>%
  plot()

or

mtcars %>>%
  subset(mpg <= quantile(mpg, 0.95)) %>>%
  lm(formula = mpg ~ wt + cyl) %>>%
  (? ~ "debugging") %>>% # print expression only
  (~ browser()) %>>%
  plot()