$ R CMD BATCH --no-save --no-restore "--args a=1 b=c(2,5,6)" test.R test.out &在test.R程序代码里面读取参数的代码如下
##First read in the arguments listed at the command line
args=(commandArgs(TRUE))
##args is now a list of character vectors
## Then cycle through each element of the list and evaluate the expressions.
for(i in 1:length(args)){
eval(parse(text=args[[i]]))
}
print(a*2)
print(b*3)
因为eval(parse(text=args[[i]]))会直接把a=1 b=c(2,5,6)转换成R命令并执行。
还有一种给参数的方法,使用Rscript
Rscript myScript.R "a=5" "b=100"
在myScript.R中也需要有
args <- commandArgs(TRUE)