如何获取读stdin的R脚本

时间:2022-08-16 20:35:20

I have the following two ways of reading from stdin. But the method using source() does not work. Does anybody know how to make the source() method work? Thanks.

我有以下两种阅读stdin的方法。但是使用source()的方法不起作用。有人知道如何使source()方法工作吗?谢谢。

~$ cat main.sh 
#!/usr/bin/env bash

set -v
cat main.csv | Rscript main.R
cat main.csv | R -q --vanilla <<EOF
source('main.R')
EOF
~$ cat main.R
f=read.csv(file('stdin'))
f
~$ ./main.sh
cat main.csv | Rscript main.R
> f=read.csv(file('stdin'))
> f
    X V1 V2
1   1  1 11
2   2  2 12
3   3  3 13
4   4  4 14
5   5  5 15
6   6  6 16
7   7  7 17
8   8  8 18
9   9  9 19
10 10 10 20
> 
cat main.csv | R -q --vanilla <<EOF
source('main.R')
EOF
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  no lines available in input
Calls: source ... withVisible -> eval -> eval -> read.csv -> read.table
Execution halted

1 个解决方案

#1


3  

This works:

如此:

cat main.csv | R -q --vanilla -e 'source("main.R")'

where the last line of main.R has been replaced with

主的最后一行。R已经被替换了。

print(f)

#1


3  

This works:

如此:

cat main.csv | R -q --vanilla -e 'source("main.R")'

where the last line of main.R has been replaced with

主的最后一行。R已经被替换了。

print(f)