[R] plotting to stdout (while reading from stdin?)

Gene Selkov selkovjr at uchicago.edu
Sun Mar 2 23:59:07 CET 2008


Hi Dirk,

I didn't at first pay attention to your comment about littler, as my 
original problem of plotting to stdout was solved. But it was just part of 
the larger problem: I actually need to be piping the data with the code 
for making the picture in, and getting the picture out without opening any 
files. This is to be run in a server environment, where the use of temp 
files is not really acceptable.

Having solved half of the problem, I feel cornered again, because I 
haven't found a nice way of mixing code and data in R.

I am aware that this issue was brought up before; I am not sure I like the 
solutions suggested, because they involve the R language parsing and 
interpreting for each data row -- if I understand correctly.

   http://tolstoy.newcastle.edu.au/R/help/03a/6750.html

Are these solutions the best currently available? That thread is almost 5 
years old now.

Let me give you an example of what I would like to be possible:

In postgres, I can read this from stdin:

    CREATE TABLE remark (
      "case" smallint,
      text text
    );

    COPY remark ("case", text) FROM stdin;
    877     lymph node biopsy
    909     Unresectable mass in the body of the pancreas
    \.

    ... more SQL ....

It allows the "code" and "input" chunks to be mixed, because the 
input for stdin is always terminated by a special token, '\.', and so the 
parser can skip the data chunks without interpreting them.

Almost similarly, perl has the __DATA__ token, that allows a portion of 
text within a program to be treated as stdin.

I wonder whether anything like this is possible in R or littler.


Thanks,

--Gene


On Fri, 7 Sep 2007, Dirk Eddelbuettel wrote:

> On Fri, Sep 07, 2007 at 02:27:00PM -0400, Duncan Murdoch wrote:
>> On 9/7/2007 2:15 PM, Gene Selkov wrote:
>>> Thanks a ton, Duncan!
>>>
>>> So I have verified that this line works:
>>>
>>>    echo "postscript(file=\"\", command=\"cat\"); plot(0)" | r --vanilla --slave
>>>
>>> Wonderful! (albeit a little unobvious)
>>
>> I would include an explicit "dev.off()" after the plotting; I'm not sure
>> all devices guarantee a clean shutdown when R quits.
>
> And for the record, both littler and Rscript can do that without the
> need for double quotes, at least under Linux.  E.g. both
>
> $ r -e 'postscript(file="", command="cat"); plot(0)' | head
> $ Rscript -e 'postscript(file="", command="cat"); plot(0)' | head
>
> provide the same output (of the beginning of the postscript output).
> Our r is as usual somewhat faster, not that this matters in this
> non-repeat context.
>
> Dirk
>
> -- 
> Three out of two people have difficulties with fractions.
>



More information about the R-help mailing list