R 语言命令行参数处理

在unix、windows外部需要调用R脚本执行,然后又需要输入不同的参数,类似shell脚本的命令行参数输入,可以使用Rcript命令实现。
命令格式:Rscript [options] [-e expression] file [args]
file表示需要执行的脚本,[options] [-e expression] 可以有也可以不用。
[args]是参数列表。
首先需要在file文件中的第一行加入:
Args <- commandArgs()
然后按照以下格式执行
Rscript *.R 参数1 参数2 ...
在file脚本中,可以引用参数Args,
Args[1]= "/usr/local/lib64/R/bin/exec/R"
Args[2]= "--slave"
Args[3]= "--no-restore"
Args[4]="--file=a.r"
Args[5]="--args"
Args[6]==参数1
Args[7]==参数2
可见输入的参数从第六个和第七个开始。
 1 #correctReads.R
 2 Args <- commandArgs()
 3 print(Args)
 4 
 5 结果输出:
 6 $ Rscript correctReads.R 1 2 3
 7 [1] "/Library/Frameworks/R.framework/Resources/bin/exec/R"
 8 [2] "--slave"
 9 [3] "--no-restore"
10 [4] "--file=correctReads.R"
11 [5] "--args"
12 [6] "1"
13 [7] "2"
14 [8] "3"
原文地址:https://www.cnblogs.com/xiaofeiIDO/p/8315948.html