问题描述
我一直在使用这里
使我的项目更具可移植性。它与我使用 cronR
到一些我的脚本。当我从Rstudio运行 my_script.R
时,我从 library(这里)
收到消息:
here()从/ home / pd / projects / my_proj
$开始b
$ b
当我将 script.R
设置为使用 cronR
运行时,消息:
here()从/ home / pd
$ p开始$ p>
my_schedule.cron
的存储位置。理想情况下,我想保留my_schedule.cron
的位置。我从日志中看到,my_script.R
在保存数据方面运行良好,因为here()
不正确。无论如何,当从<$ c $运行时,是否可以获得 here
函数来检测项目目录。 c> cronR 还是终端?解决方案了解如何使用环境变量
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 usecronR
to schedule some of my scripts. When I runmy_script.R
from Rstudio I get a message fromlibrary(here)
:here() starts at /home/pd/projects/my_proj
When I set
script.R
to run usingcronR
I get a different message:here() starts at /home/pd
Which is where
my_schedule.cron
is stored. Ideally I want to keepmy_schedule.cron
where it is. I can see from the logs thatmy_script.R
runs fine apart from when it comes to saving data because the path used byhere()
is incorrect. Is there anyway to get thehere
function to detect the project dir whenmy_script.R
is run fromcronR
or the terminal?解决方案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计划中使用此处包的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!