renkun-ken / pipeR

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

Consider value piping for Pipe #25

Closed renkun-ken closed 10 years ago

renkun-ken commented 10 years ago

Consider supporting the following piping mechanism for Pipe(x)$y

  1. Look for any y in Pipe object environment ($value, $fun)
  2. If x in Pipe is S4, get slot y from x.
  3. If x in Pipe is list or vector, get element named y from x.
  4. Otherwise, look for function y in parent.frame()

This allows the following usage:

> Pipe(mtcars)$fun(lm(mpg~.,data=.))$summary()$fstatistic
$value : numeric 
------
   value    numdf    dendf 
13.93246 10.00000 21.00000 
renkun-ken commented 10 years ago

For example,

library(fUnitRoots)
Pipe(rnorm(100))$
  cumsum()$
  adfTest(lags = 5)$
  test$
  statistic
$value : numeric 
------
Dickey-Fuller 
    -2.033778 
renkun-ken commented 10 years ago

This implementation often leads to potential confusion.

Consider a data frame containing a column named select, if dplyr is used to select the columns, the column will be singled out and select() would be ignored.

If rule 4 has higher priority, $ is still overloaded by too much potential functionality.

renkun-ken commented 10 years ago

A new closure .() is added to Pipe object. For .(y),

This allows the following usage:

Pipe(mtcars)$
  .(lm(mpg ~ cyl + wt, data = .))$
  .(coefficients)
$value : numeric 
------
(Intercept)         cyl          wt 
  39.686261   -1.507795   -3.190972 

For S4 object,

library(fUnitRoots)
Pipe(rnorm(100))$
  cumsum()$
  adfTest(lags = 5)$
  .(test)$
  .(statistic)
$value : numeric 
------
Dickey-Fuller 
    -1.283631 

Then .() incorporates the functionality of fun() which may be deprecated in future version for potential name conflicts.

renkun-ken commented 10 years ago

Now, Pipe(x)$.(...) closure in Pipe object works in exactly the same way with x %>>% (...).