Piping stdin to R

2019-01-08 19:17发布

问题:

I am having trouble piping stdin to an R script.

Here is my toy script test.R:

#!/usr/bin/env Rscript
while(length(line <- readLines('stdin', n=1, warn=FALSE)) > 0) {
  write(line, stderr())
  # process line
}

I'd like to go through each line and do some processing. Here is my input file named input:

aaaaaa
bbbbbb
cccccc
dddddd
eeeeee
ffffff

If I do

cat input | test.R

I only get:

aaaaaa

Is there anything that I missed?

回答1:

This does not happen if you explicitly open the stdin connection.

#!/usr/bin/env Rscript
f <- file("stdin")
open(f)
while(length(line <- readLines(f,n=1)) > 0) {
  write(line, stderr())
  # process line
}


回答2:

Jeff and I wrote littler to do just this (and a few other things). Because of littler, I never looked that closely at Rscript -- but this should in principle work just fine.

Here is one of our early examples, using output from /bin/ls (and a quick filter by awk) to summarize file size:

edd@max:~/svn/littler/examples$ ls -l /boot/ | \
                                    awk '!/^total/ {print $5}' | ./fsizes.r 
    Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
      24   130300   730700  3336000  4527000 14670000 

  The decimal point is 6 digit(s) to the right of the |

   0 | 0000000000000011111111122777777777
   2 | 777777777
   4 | 555577777
   6 | 
   8 | 
  10 | 
  12 | 5
  14 | 24466677

edd@max:~/svn/littler/examples$ 

Here the script fsizes.r is just three lines:

edd@max:~/svn/littler/examples$ cat fsizes.r 
#!/usr/bin/r -i

fsizes <- as.integer(readLines())
print(summary(fsizes))
stem(fsizes)
edd@max:~/svn/littler/examples$ 


回答3:

This is the easiest I've found (assuming numeric input):

x <- scan(file="stdin", quiet=TRUE)

you can test it with:

$ echo -e "1\n2\n3" | R --slave -e 'x <- scan(file="stdin", quiet=TRUE); summary(x)'
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    1.0     1.5     2.0     2.0     2.5     3.0 


标签: r shell pipe