Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R脚本报告错误的命令行参数_R_Command Line Arguments - Fatal编程技术网

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

显然,在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]

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。我错了,谢谢