【问题标题】:How to get the script path in R? [duplicate]如何获取R中的脚本路径? [复制]
【发布时间】:2011-12-26 15:04:57
【问题描述】:

可能重复:
Rscript: Determine path of the executing script

我总是运行一个 R 脚本,它将 My Documents 作为工作目录。

当我运行脚本时,我想获取脚本路径并将工作目录更改为它。我该怎么做?

我希望它使用 source() 函数在同一文件夹中运行脚本。

【问题讨论】:

  • 如果您使用 ESS,则有一个宏可以执行此操作。可能也适用于其他编辑。
  • 也许:args <- commandArgs(trailingOnly = F) ; scriptPath <- dirname(sub("--file=","",args[grep("--file",args)])) 来自上面引用的 SO 链接中的答案之一。

标签: r path directory


【解决方案1】:

如果您通过source 运行脚本,请尝试source(file, chdir = TRUE)

【讨论】:

    【解决方案2】:

    什么十六进制,我尝试将其作为解决方案:

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

    【讨论】:

      猜你喜欢
      • 2016-07-26
      • 1970-01-01
      • 2011-07-15
      • 2010-10-10
      • 2011-01-10
      • 1970-01-01
      • 2015-10-07
      • 2011-08-21
      相关资源
      最近更新 更多