使用bash脚本中的参数运行R文件

lis*_*nne 1 bash r

可能重复:
如何从R脚本中读取命令行参数?

我有一个R脚本,我希望能够提供几个命令行参数(而不是代码本身的硬编​​码参数值).该脚本在linux上运行.

我无法找到如何在命令行Bash上读取R.script.

sh文件

cd `dirname $0`
/usr/lib64/R/bin/R --vanilla --slave "--args input='$1' input2='$2' output='$3'"  file=/home/lvijfhuizen/galaxy_dist/tools/lisanne/partone.R  $3.txt
Run Code Online (Sandbox Code Playgroud)

R档

args <- commandArgs()
file <- read.csv(args[8],head=TRUE,sep="\t")   
annfile <- read.csv(args[9],head=TRUE,sep="\t")
Run Code Online (Sandbox Code Playgroud)

jub*_*uba 5

要从命令行获取R脚本,可以将其通过管道传输到R中<.

例如,如果我创建以下test.shbash脚本:

#!/bin/bash

rfile=$1
shift
R --vanilla --slave --args $* < $rfile
exit 0
Run Code Online (Sandbox Code Playgroud)

test.R同一目录中的以下R脚本在哪里:

print(commandArgs(trailingOnly=TRUE))
Run Code Online (Sandbox Code Playgroud)

然后运行脚本test.R作为第一个参数,可能还有其他人会给出这样的东西:

$ ./test.sh test.R foo bar 1 2 3
[1] "foo" "bar" "1"   "2"   "3"  
Run Code Online (Sandbox Code Playgroud)

编辑:另一种方式,也许更清洁,是使用专用Rscript命令.然后你可以直接在你的bash脚本中添加如下内容:

rfile=$1
shift
Rscript $rfile $*
Run Code Online (Sandbox Code Playgroud)

这应该给出相同的结果.