bash 中的命令行参数到 Rscript
我有一个 bash 脚本,它创建一个 csv 文件和一个从中创建图形的 R 文件。
在 bash 脚本的末尾,我调用 Rscript Graphs.R 10
我得到的响应如下:
Error in is.vector(X) : subscript out of bounds
Calls: print ... <Anonymous> -> lapply -> FUN -> lapply -> is.vector
Execution halted
我的 Graphs.R 的前几行是:
#!/bin/Rscript
args <- commandArgs(TRUE)
CorrAns = args[1]
不知道我做错了什么?在我看来,网上的建议应该可行。很难理解 commandArgs
I have a bash script that creates a csv file and an R file that creates graphs from that.
At the end of the bash script I call Rscript Graphs.R 10
The response I get is as follows:
Error in is.vector(X) : subscript out of bounds
Calls: print ... <Anonymous> -> lapply -> FUN -> lapply -> is.vector
Execution halted
The first few lines of my Graphs.R are:
#!/bin/Rscript
args <- commandArgs(TRUE)
CorrAns = args[1]
No idea what I am doing wrong? The advice on the net appears to me to say that this should work. Its very hard to make sense of commandArgs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
通过 args.R 中的以下内容
和 args.sh 中的以下内容,
我从 bash args.sh 获得以下输出
,并且没有错误。如有必要,请使用
as.numeric(commandArgs(TRUE)[1])
转换为数字类型。With the following in args.R
and the following in args.sh
I get the following output from
bash args.sh
and no error. If necessary, convert to a numberic type using
as.numeric(commandArgs(TRUE)[1])
.只是猜测,也许您需要将
CorrAns
从字符转换为数字,因为?CommandArgs
的 Value 部分表示:更新:它可能很简单:
Just a guess, perhaps you need to convert
CorrAns
from character to numeric, since Value section of?CommandArgs
says:UPDATE: It could be as easy as:
阅读文档,看来你可能需要从对
commandArgs()
的调用中删除TRUE
,因为您不使用--args
调用脚本。要么这样,要么您需要调用 Rscript Graphs.R --args 10 。Reading the docs, it seems you might need to remove the
TRUE
from the call tocommandArgs()
as you don't call the script with--args
. Either that, or you need to callRscript Graphs.R --args 10
.Rscript args.R 10
其中 10 是我们要传递给 R 脚本的数值。print(as.numeric(commandArgs(TRUE)[1])
打印出可以分配给变量的值。Rscript args.R 10
where 10 is the numeric value we want to pass to the R script.print(as.numeric(commandArgs(TRUE)[1])
prints out the value which can then be assigned to a variable.