【问题标题】:How do I get in R the name of currently executed script when called via `r BATCH script file`通过`r BATCH脚本文件`调用时,如何在R中获取当前执行的脚本的名称
【发布时间】:2013-11-16 01:48:06
【问题描述】:

我用

从 shell 命令行调用脚本
r CMD BATCH myscript.R

我必须在 myscript.R 中做什么才能获得带有脚本名称的字符向量(即“myscript.R”)?

注意:

我发现了许多关于类似主题的问题,但找不到任何适合我的东西。特别是从问题Determine path of the executing script我得到了修改后的脚本sn-p:

args <- commandArgs(trailingOnly = F)  
scriptPath <- sub("^--file=", "", args[grep("^--file=", args)])

scriptPath 始终为空(可能是由于我通过 BATCH 命令调用脚本的方式)。

【问题讨论】:

    标签: r batch-file filepath


    【解决方案1】:

    commandArgs() 的简单示例对我来说效果很好:

    myscript.R:

    commandArgs()[3]
    

    在终端:

    R CMD BATCH myscript.R
    

    然后cat myscript.Rout:

     commandArgs()[3]
    [1] "myscript.R"
    

    【讨论】:

    • 也适合我。
    • 完美运行。谢谢大家。
    【解决方案2】:

    我相信如果您使用 Rscript 而不是 R CMD BATCH 它会为您工作。当我试图弄清楚如何将工作目录设置为 myscript 的目录时,我阅读了同一篇文章。无论如何要运行Rscript

    "....\R-3.0.1\bin\x64\Rscript.exe" Myscript.r &gt; Myscript.r.rout

    这是我将工作目录设置为脚本目录的代码。我一直在尝试不同的替代方案,然后我意识到你需要使用 Rscript 才能使它们中的大部分工作。

    args <- commandArgs(trailingOnly = F)  
    scriptPath <- normalizePath(dirname(sub("^--file=", "", args[grep("^--file=", args)])))
    setwd(scriptPath)
    

    【讨论】:

      猜你喜欢
      • 2011-01-12
      • 2011-10-15
      • 2017-09-09
      • 2012-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-04
      相关资源
      最近更新 更多