[R] Using pipe for input data

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 6 08:22:57 CEST 2004

The C stdin is used *always* to read commands from on Unix R, and even on 
console versions stdin() is where the commands are read from.  

R CMD BATCH is approximately giving you

R --vanilla --slave < my.R

and piping to such a command is going to do nothing for you.
Your command read.table(stdin() ... is going to read from the script my.R.

On Thu, 5 Aug 2004, Hayashi Soichi - shayas wrote:

> I have asked this question before and Aaron J. Mackey and Tony Plate gave me
> some great insight but I still can't figure out how to do what I am trying
> to accomplish. So let me ask again...
> What I am trying to do is to make R read data from pipe (stdin).
> Say I have following files on my directory
> my.dat
>        apple 1
>        orange 2
>        grape 3
> my.R
>        d <- read.table( stdin(), header=F, dec='.',
> col.names=c("name","type"), na.strings=c("xxxx"))
>        summary(d)
> and When I run this command
>        cat my.dat | R CMD BATCH --vanilla --slave my.R
> I am expecting to see the summery report for the datasource my.dat
> But here is what I actually see in my.Rout
> 	> d <- read.table( stdin(), header=F, dec='.',
> col.names=c("name","type"), na.strings=c("xxxx"))
> 	0: summary(d)
> 	1: proc.time()
> 	2: Error in scan(file = file, what = what, sep = sep, quote = quote,
> dec = dec,  :
> 	line 1 did not have 2 elements 
> 	Execution halted
> If I execute the content of the my.R on regular R command line, I can
> actually "type in" all datasource and creates the correct summery report. So
> I don't know why I can make R to read the input from the piped datasource...

See the above analysis.  What I don't know is why you expected this to
work: did you look at the sources, e.g. the file BATCH?  If not - `great
insight' - the sources are the definitive documentation.

Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

More information about the R-help mailing list