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

From: Gene Selkov <selkovjr_at_uchicago.edu>
Date: Sun, 02 Mar 2008 16:59:07 -0600 (CST)

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.
>

R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Sun 02 Mar 2008 - 23:27:57 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 03 Mar 2008 - 01:30:17 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive