【发布时间】:2023-03-09 21:45:02
【问题描述】:
假设我使用R --arg1=1 --arg2=2 启动 R,我可以从 R 会话本身中找到这些参数吗?
【问题讨论】:
标签: r environment-variables startup
假设我使用R --arg1=1 --arg2=2 启动 R,我可以从 R 会话本身中找到这些参数吗?
【问题讨论】:
标签: r environment-variables startup
命令是commandArgs(),
Provides access to a copy of the command line arguments supplied when this R session was invoked.
因此,要将命令行参数分配给 R 中的对象,您可以这样做..
args <- commandArgs(TRUE)
TRUE 是可选的,但只会在--args 选项之后立即返回您在命令行上传递的参数。 FALSE 也返回一大堆其他信息。
$ R CMD BATCH '--args a=1 b=2' myscript.R myscript.Rout
commandArgs(TRUE)
#a b
#1 2
【讨论】:
R --vanilla 和 R 内 commandArgs(TRUE) 什么都没有……我也不是在谈论 Rscript,而是在说普通的 R!
commandArgs(FALSE) 或只是commandArgs()。当TRUE 时,它只提供 --args 中提供的参数,没有别的。