Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to use here package with cronR scheduling

Tags:

r

cron

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?

like image 661
Pete900 Avatar asked Oct 12 '18 08:10

Pete900


1 Answers

You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:

cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
like image 194
tomaz Avatar answered Sep 23 '22 14:09

tomaz