R脚本报告错误的命令行参数
显然,在R中,可以使用R脚本报告错误的命令行参数,r,command-line-arguments,R,Command Line Arguments,显然,在R中,可以使用commandArgs()函数获取args 让我们试试看。文件名为wtf.r: #!/usr/bin/Rscript args = commandArgs() asdf = list() asdf$arg1 = args[2] # Starting from 2 bc IIUC the first element is supposed to be the program's name asdf$arg2 = args[3] asdf$arg3 = args[4] p
commandArgs()
函数获取args
让我们试试看。文件名为wtf.r
:
#!/usr/bin/Rscript
args = commandArgs()
asdf = list()
asdf$arg1 = args[2] # Starting from 2 bc IIUC the first element is supposed to be the program's name
asdf$arg2 = args[3]
asdf$arg3 = args[4]
print(asdf)
让我们试着运行它:
my@comp:~/wtfdir$ ./wtf.r arg1 arg2 arg3
$arg1
[1] "--slave"
$arg2
[1] "--no-restore"
$arg3
[1] "--file=./wtf.r"
什么..啊
为什么arg1
设置为--slave
?为什么将arg2
设置为--无恢复
?为什么arg3
设置为--file=./wtf.r
?!我不记得在命令行中输入了任何类似的内容
这里出了什么问题?你可能想要
commandArgs(trailingOnly=TRUE)
?@joran我很惊讶。作品我认为忽略一些用户提供的参数是一个方便的选择。ie我想如果我像/wtf.r arg1--args arg2 arg3
那样调用我的脚本,那么trailingOnly=TRUE
将忽略arg1
,只报告arg2
和arg3
,而trailingOnly=FALSE
将报告所有arg。我错了,谢谢你也许想要commandArgs(trailingOnly=TRUE)
?@joran我很惊讶。作品我认为忽略一些用户提供的参数是一个方便的选择。ie我想如果我像/wtf.r arg1--args arg2 arg3
那样调用我的脚本,那么trailingOnly=TRUE
将忽略arg1
,只报告arg2
和arg3
,而trailingOnly=FALSE
将报告所有arg。我错了,谢谢