2

Created a simple project using Rstudio and included renv. It simply creates txt file in project directory.

enter image description here

created batch file:

CALL "C:\Program Files\R\R-4.3.2\bin\Rscript.exe" -e "renv::load('C:/Users/username/R/ProjectFolder'); source('C:/Users/username/R/Test1/Test1.R')"> "C:/Users/username/R/Test1/Output.log" 2>&1   
exit

If I double click on batch file - works fine.

But if I use task scheduler to run batch file it gives me an error:

Error in loadNamespace(x) : there is no package called 'renv'
Calls: loadNamespace -> withRestarts -> withOneRestart -> doWithOneRestart
Execution halted

Seems like it does not activate renv.

Task scheduler snip:

enter image description here

4
  • 1
    It sounds like the task scheduler is not starting R in the project directory. It's the .Rprofile file (I believe) that sets up the environment so that renv is found. Do you have a way in task-scheduler to set the working directory to that proj dir? Commented Feb 7, 2024 at 22:18
  • I dont think I ever set working directory in task scheduler. Snip attached. So is any way i can do it in .Rprofile file? Commented Feb 7, 2024 at 22:58
  • this is the only line of code my .Rprofile file has: source("renv/activate.R") Commented Feb 7, 2024 at 23:00
  • 1
    Yes, and that file is not run if the Task Scheduler does not use that project directory as its working directory. serverfault.com/q/609651/316496 is relevant, pointing out that it is clearly not automatically what you want it to be. I suggest you start your batch script with something like CD "C:\Users\username\R\ProjectFolder" (and then the CALL ... on the next line). Commented Feb 7, 2024 at 23:32

1 Answer 1

3

renv functionality requires that the R process start in the project directory, calling Rscript -e /some/path/to/file.R does not accomplish that. In Task Scheduler, per https://serverfault.com/q/609651/316496, jobs tend to start in %Windir%\System32\, not your project directory.

Modify your script so that it first changes to the appropriate directory and then calls Rscript, perhaps

CD C:\Users\username\R\ProjectFolder
CALL "C:\Program Files\R\R-4.3.2\bin\Rscript.exe" -e "renv::load('C:/Users/username/R/ProjectFolder'); source('C:/Users/username/R/Test1/Test1.R')"> "C:/Users/username/R/Test1/Output.log" 2>&1   
exit

(I'm not certain if R\ProjectFolder\ or R\Test1\ is the project with .Rprofile and renv's stuff, adjust as appropriate.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.