How to play nice with taskscheduler, R Studio Projects and here

Hey R users,

I have a project that I built using R studio projects which works fine from start to finish when I run it through R studio, both in my dev environment as well as production. However in the prod environment, I need it to be scheduled through the taskscheduleR package.

Unfortunately my prod environment is a windows machine and taskscheduler kicks off the script using Rscripts.exe and all me .R files which use the here package to navigate through the project directory structure fail because the default working directory is C:/Program Files/R/R-3.4.1/bin

I thought adding a setwd(here::here()) would search for the .here file in the location of the file that Rscripts.exe is calling, but it doesn't. I feel really dirty to hard code a setwd() just to get this script scheduled.

What are the best ways of handling such a situations?

Thanks in advance for your help!

3 Likes

Have you tried looking into Windows Task Scheduler options?

Look for the scheduled task, right click > Properties. Go to Actions tab, then Edit.

In the ensuing dialog box, you can set the working directory to start from, so all relative paths in your R script are rooted from here.

image

4 Likes

That's perfect, thanks so much!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.