【问题标题】:How can I check which startup arguments was R started with如何检查 R 启动的启动参数
【发布时间】:2023-03-09 21:45:02
【问题描述】:

假设我使用R --arg1=1 --arg2=2 启动 R,我可以从 R 会话本身中找到这些参数吗?

【问题讨论】:

    标签: r environment-variables startup


    【解决方案1】:

    命令是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
    

    R 内

    commandArgs(TRUE)
    #a b 
    #1 2 
    

    【讨论】:

    • 实际上这真的有效吗? R --vanilla 和 R 内 commandArgs(TRUE) 什么都没有……我也不是在谈论 Rscript,而是在说普通的 R!
    • @statquant 重新阅读答案。现在您要求commandArgs(FALSE) 或只是commandArgs()。当TRUE 时,它只提供 --args 中提供的参数,没有别的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2015-01-13
    相关资源
    最近更新 更多