renkun-ken / pipeR

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

Option to not print Pipe #22

Closed ramnathv closed 10 years ago

ramnathv commented 10 years ago

I am really enjoying this package and the Pipe function. One thing I noticed is that whenever I use Pipe, it explicitly prints the word Pipe in my R console. While this is a useful message, I want to be able to suppress it. Is that possible currently, or can an option be added?

renkun-ken commented 10 years ago

I'm thinking of this too :) Pipe function creates a Pipe object that is in essence an environment containing a value and its $, [ and print methods are implemented. I guess if the message is suppressed, users without much experience would probably mistakenly take the environment as the value.

In the latest version in branch 0.4, the output is:

> Pipe(1:3)$as.list()
<Pipe>
$value : list 
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

Do you mean that <Pipe> can be suppressed so that the output can be something like ...?

> Pipe(1:3)$as.list()
$value : list 
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

This looks nice too :) and maybe users won't get it wrong to take it as the value itself.

renkun-ken commented 10 years ago

I change the code locally and find it looks pretty simple and nice. Does this work for you?

> Pipe(mtcars)$fun(lm(mpg~cyl,data=.))$summary()
$value : summary.lm 

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

Residuals:
    Min      1Q  Median      3Q     Max 
-4.9814 -2.1185  0.2217  1.0717  7.5186 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  37.8846     2.0738   18.27  < 2e-16 ***
cyl          -2.8758     0.3224   -8.92 6.11e-10 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 3.206 on 30 degrees of freedom
Multiple R-squared:  0.7262,    Adjusted R-squared:  0.7171 
F-statistic: 79.56 on 1 and 30 DF,  p-value: 6.113e-10
ramnathv commented 10 years ago

I will test it. Another request I had was for an unPipe operator. Currently, when I pipe through stuff and save it to an object and do str I dont see the underlying structure. I can file a separate issue if you would prefer that.

Great work!!

ramnathv commented 10 years ago

I tested it. I was looking for something that doesn't do any cat. Just print final value. Maybe it could be an option for Pipe.

renkun-ken commented 10 years ago

I guess the problem is that if it does not do any cat, ordinary users will mistakenly regard the Pipe object as the value stored in it.

Consider the following usage:

> Pipe(list(a=1,b=2,c=3))$unlist()
$value : numeric 
a b c 
1 2 3 

If any cat is suppressed like

> Pipe(list(a=1,b=2,c=3))$unlist()
a b c 
1 2 3 

I think an ordinary user would think that the value must be the numeric vector and directly use it for other work like

> z <- Pipe(list(a=1,b=2,c=3))$unlist()
> z + 1
Error in z + 1 : non-numeric argument to binary operator

The problem is that it is not the value itself but another Pipe object which is exactly the trick to make chaining possible. So to unpipe, or extract the value, just call z$value or simply z [].

> Pipe(list(a=1,b=2,c=3))$unlist()$value
a b c 
1 2 3 
> Pipe(list(a=1,b=2,c=3))$unlist() []
a b c 
1 2 3 

These are the true value stored in Pipe object. print.Pipe only allows users to preview the value stored in it. To use it, one has to extract the value.

For you str usage, I can implement str method for Pipe as str.Pipe so it works like:

> str(Pipe(1:3))
$value :
 int [1:3] 1 2 3

Does that work for you?

An option to suppress any header is OK, but it won't be turned on by default in case users are confused.

renkun-ken commented 10 years ago

Please try the latest commit with setting the following option:

options(Pipe.header = FALSE)

An example is

> Pipe(list(a=1,b=2))$unlist()
a b 
1 2 

Does that work for you?

ramnathv commented 10 years ago

@renkun-ken I should have probably explained my use case better. I am looking to use Pipe with chains that produce a plot at the end.

For example

Pipe(mtcars)$
  ggvis(~mpg, ~wt)

In this case, I only expect to see a plot, but Pipe prints the message Pipe in the R console. What I was asking was for an option to not print the Pipe.

renkun-ken commented 10 years ago

@ramnathv, that makes sense now. The latest version at 0.4 branch supports option Pipe.header so you can set Pipe.header = FALSE to suppress all possible cats. Or otherwise you may only use print(..., header = FALSE) to suppress printing headers case by case.

In the case where ggvis() is used to plot mtcars, the function actually returns ggvis object whose print method evaluates (or draws) the plot in the Viewer pane. So far I don't know how to "smartly" suppress cat if a plot is produced because the returned value is really a ggvis object and the plot is a side effect.

ramnathv commented 10 years ago

Perfect! I was installing the master branch before, and so kept getting the same output. I switched to using the 0.4 branch and the Pipe.header options solves the issue. Thanks!

renkun-ken commented 10 years ago

After some thinking, I'd say that the right way to suppress the cat message should be like

> Pipe(mtcars)$
+   ggvis(~mpg, ~wt) []
Guessing layer_points()
... a scatter plot is produced ...

If you want the plot to be the final result and avoid further piping, you may better extract the value, i.e. the plot itself, rather than ending up with ggvis(~mpg, ~wt) which waits for further piping (that's why it says it's a Pipe and its value is ggvis).

Pipe.header = FALSE is a valid walk-around, but I'd not recommend this to ordinary user in case of potential confusion. I'd recommend using [] or $value to extract the value so that no message will be cat because it is not the Pipe object that is being printed but the value itself. In this case, ggvis is printed as the image without any cat message.

A recommended way to use Pipe that avoids any side effect using it is to start with Pipe() and end with [] or $value. Hope my explanation helps :)

renkun-ken commented 10 years ago

I'm still not sure if Pipe.header will finally go to master if the above comment solves your problem. Let me know if you really need that option which seems to be an overkill given the usage of [] or $value.

renkun-ken commented 10 years ago

merged to master.