在cronR计划中使用此处包的正确方法 [英] Correct way to use here package with cronR scheduling

查看:140
本文介绍了在cronR计划中使用此处包的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用这里 软件包使我的项目更具可移植性。它与我使用 cronR 安排一些我的脚本。当我从Rstudio运行 my_script.R 时,我从 library(这里)收到消息:

  here()从/ home / pd / projects / my_proj 

$开始b
$ b

当我将 script.R 设置为使用 cronR 运行时,消息:

  here()从/ home / pd 


my_schedule.cron 的存储位置。理想情况下,我想保留 my_schedule.cron 的位置。我从日志中看到, my_script.R 在保存数据方面运行良好,因为 here()不正确。无论如何,当从<$ c $运行时,是否可以获得 here 函数来检测项目目录。 c> cronR 还是终端?

解决方案


如果第一行您的#rstats脚本中的wd<-here(),我将
进入您的实验室并在着火时设置计算机。


了解如何使用环境变量

  wd<-Sys.getenv( HOME)
wd<-file.path(wd, projects, my_proj)

或使用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]
}


I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):

here() starts at /home/pd/projects/my_proj

When I set script.R to run using cronR I get a different message:

here() starts at /home/pd

Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?

解决方案

If the first line of your #rstats script is wd <- here(), I will come into your lab and SET YOUR COMPUTER ON FIRE.

Learn how to use environment variables

wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")

Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs(). If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")

args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
  wd <- args[1]
}

这篇关于在cronR计划中使用此处包的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆