如何在R脚本中读取管道中的stdin?

时间:2013-08-28 21:14:22

标签: r

我试图绘制一个令人作呕的情节(http://www.cbs.dtu.dk/~eklund/beeswarm/),然后我开始工作了,现在我想写一个小的R脚本来实现自动化。我要给这个R脚本的输入来自STDIN,我很难从STDIN读取数据。

这是我的R脚本:

args <- commandArgs(TRUE)
f1 <- args[1]

plotbeeswarm <- function(output){
    library(beeswarm)
    f <- read.table(stdin(), header=TRUE)
    png(output, width=800, height=800)
    beeswarm(Data ~ Category, data=f, pch=16, pwcol=1+as.numeric(sample), 
             xlab="")
}

plotbeeswarm(f1)

我认为问题在于如何读取输入文件并将其处理为f。任何人都可以帮我修复我的代码吗?非常感谢!

1 个答案:

答案 0 :(得分:2)

以下是我在littler的网页上使用的示例,您也应该能够适应Rscript

脚本的代码只是:

#!/usr/bin/r -i

fsizes <- as.integer(readLines(file("stdin")))
print(summary(fsizes))
stem(fsizes)

我将ls -l的结果反馈到awk过滤,只得到一列文件大小:

edd@max:~/svn/littler/examples$ ls -l /bin/ | awk '{print $5}' | ./fsizes.r 
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.    NA's 
      3    6240   30820   61810   60170 2000000       1 

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

   0 | 00000000000000000000000000000000000111111111111111111111111122222222+57
   1 | 111112222345679
   2 | 7
   3 | 1
   4 | 1
   5 | 
   6 | 
   7 | 
   8 | 
   9 | 6
  10 | 
  11 | 
  12 | 
  13 | 
  14 | 
  15 | 
  16 | 
  17 | 
  18 | 
  19 | 
  20 | 0

edd@max:~/svn/littler/examples$ 
相关问题