[R] Using Rscript to read from a file
Saptarshi Guha
saptarshi.guha at gmail.com
Fri Oct 15 06:04:15 CEST 2010
Dirk, thanks.
On Thu, Oct 14, 2010 at 9:42 AM, Dirk Eddelbuettel <edd at debian.org> wrote:
>
> On 14 October 2010 at 11:21, Dirk Eddelbuettel wrote:
> | 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.
>
> Got it. We changed something since we first created this example in 2006, and
> we now need to set the -i flags. So with that:
>
> $ ls -l /usr/bin | awk '!/^total/ {print $5}' | \
> r -i -e 'fsizes <- as.integer(readLines()); print(summary(fsizes))'
> Min. 1st Qu. Median Mean 3rd Qu. Max.
> 1 1720 10900 108000 39200 11100000
> $
>
> Dirk
>
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>
More information about the R-help
mailing list