将标准输入到R [英] Piping stdin to R

查看:64
本文介绍了将标准输入到R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法将stdin传递到R脚本.

I am having trouble piping stdin to an R script.

这是我的玩具脚本test.R:

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

我想遍历每行并做一些处理.这是我的输入文件,名为input:

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

如果我愿意

cat input | test.R

我只会得到:

aaaaaa

有什么我想念的吗?

推荐答案

如果您明确打开stdin连接,则不会发生这种情况.

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
}

这篇关于将标准输入到R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆