[R] Using Rscript to read from a file
Dirk Eddelbuettel
edd at debian.org
Thu Oct 14 18:21:13 CEST 2010
On 14 October 2010 at 08:49, Saptarshi Guha wrote:
| Hello,
| I have this script which will be invoked as
|
| Rscript a.r < a.r
|
| a.r follows
|
| #!/usr/bin/Rscript --vanilla
| f=file("stdin")
| while(TRUE){
| y=readLines(f,n=1,warn=TRUE)
| if(length(y)==0) break else print(y)
| }
|
|
| But it only reads one line from a.r
| How can I read line by line from standard input?
If you can substitute in r (from littler) for Rscript, then you get proper
behaviour. Here is an old example from littler:
$ ls -l /usr/bin | awk '!/^total/ {print $5}' | \
r -e 'fsizes <- as.integer(readLines()); print(summary(fsizes))' | \
tail -2
Min. 1st Qu. Median Mean 3rd Qu. Max.
1 1720 10900 108000 39200 11100000
$
This uses awk to get the file size from ls, and then runs R's summary() over
it. For some reason, readLines() now echos back to the screen which is why I
added the 'tail -2' at the end.
Dirk
--
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
More information about the R-help
mailing list