使用带有 cronR 调度的 here 包的正确方法

Pet*_*900 5 cron r

我一直在使用这个here 来让我的项目更便携。除了我cronR用来安排我的一些脚本之外,它的效果很好。当我my_script.R从 Rstudio运行时,我收到一条消息library(here)

here() starts at /home/pd/projects/my_proj
Run Code Online (Sandbox Code Playgroud)

当我设置script.R运行 using 时,cronR我收到一条不同的消息:

here() starts at /home/pd
Run Code Online (Sandbox Code Playgroud)

my_schedule.cron是存储的地方。理想情况下,我想保持my_schedule.cron原样。my_script.R除了保存数据之外,我可以从日志中看到运行良好的日志,因为使用的路径here()不正确。无论如何,是否可以在从终端或终端运行here时获得检测项目目录的功能?my_script.RcronR

小智 1

如果你的 #rstats 脚本的第一行是 wd <- here(),我将进入你的实验室并点燃你的计算机。

了解如何使用环境变量

wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Run Code Online (Sandbox Code Playgroud)

或者使用 cronR 用户界面中的“Rscript 的附加参数”元素将额外的内容传递给 Rscript 并使用 commandArgs() 获取它。如果您不使用 cronR 接口而是使用 cron_rscript,请使用cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")

args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
  wd <- args[1]
}
Run Code Online (Sandbox Code Playgroud)