[R] read input from STDIN

Jonathan Baron baron at cattell.psych.upenn.edu
Thu Dec 27 22:33:52 CET 2001

>From: yiping.fan at syngenta.com
>   I have a perl program which produces the input. Instead of print it to a
>file then let R read the file,
>I want to let R to read the input directly from the perl output, 
>I am using  a PERL IPC::open2 module for this, 
>local (*Read,*Writer);
>$pid = open2(\*Read, \*Writer, "R --no-save --slave< my.R")
>#input to R
>  print Writer data;
>close Writer;
>#R output
>@result = <Read>;
>How can "my.R"  read the stdin and figure out the relationship in the data?
>I tried realLines(), does not work.  Thank you very much for your help!!

I haven't tried this from within Perl, however, you can use R
from a Unix command line like this:
R --vanilla < my.R

To see what the --vanilla switch does that your commands do not
do, see the man page for R.  (I'm not sure which parts are the
important ones, but this works.)

To get the output from R, I write to a file from R itself, using
write() or whatever.  I'm sure there are other ways, but it seems
you have no objection to using files.

See pay.R at the end of http://finzi.psych.upenn.edu/~baron/
for an unnecessarily complex example, in which R is used to
write checks!  (You can also use it to wash the dishes :) )
I used to do this sort of thing with Perl, but, really, R is
just a better language, even for text processing, IMHO.

Jon Baron
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list