bash 中的命令行参数到 Rscript

发布于 2024-10-09 14:59:19 字数 480 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后eg是否自 2024-10-16 14:59:19

通过 args.R 中的以下内容

print(commandArgs(TRUE)[1])

和 args.sh 中的以下内容,

Rscript args.R 10

我从 bash args.sh 获得以下输出

[1] "10"

,并且没有错误。如有必要,请使用 as.numeric(commandArgs(TRUE)[1]) 转换为数字类型。

With the following in args.R

print(commandArgs(TRUE)[1])

and the following in args.sh

Rscript args.R 10

I get the following output from bash args.sh

[1] "10"

and no error. If necessary, convert to a numberic type using as.numeric(commandArgs(TRUE)[1]).

蓝戈者 2024-10-16 14:59:19

只是猜测,也许您需要将 CorrAns 从字符转换为数字,因为 ?CommandArgs 的 Value 部分表示:

包含名称的字符向量
可执行文件和
用户提供的命令行参数。

更新:它可能很简单:

#!/bin/Rscript
args <- commandArgs(TRUE)
(CorrAns = args[1])
(CorrAns = as.numeric(args[1]))

Just a guess, perhaps you need to convert CorrAns from character to numeric, since Value section of ?CommandArgs says:

A character vector containing the name
of the executable and the
user-supplied command line arguments.

UPDATE: It could be as easy as:

#!/bin/Rscript
args <- commandArgs(TRUE)
(CorrAns = args[1])
(CorrAns = as.numeric(args[1]))
云柯 2024-10-16 14:59:19

阅读文档,看来你可能需要从对 commandArgs() 的调用中删除 TRUE,因为您不使用 --args 调用脚本。要么这样,要么您需要调用 Rscript Graphs.R --args 10 。

使用

commandArgs(trailingOnly = FALSE)

参数

trailingOnly 逻辑。应该只
返回 --args 之后的参数?

Reading the docs, it seems you might need to remove the TRUE from the call to commandArgs() as you don't call the script with --args. Either that, or you need to call Rscript Graphs.R --args 10.

Usage

commandArgs(trailingOnly = FALSE)

Arguments

trailingOnly logical. Should only
arguments after --args be returned?

拥有 2024-10-16 14:59:19

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文