2

I have looked through all solutions for Automating Rscripts and I am able to work few things but my task opens Rstudio but never runs the script.

enter image description here

Please let me know if any more details are required.

4
  • 3
    You should use Rscript.exe and put it in a barch file. See this for example. Commented Jul 12, 2015 at 19:41
  • you need to run the command line version - you are just opening the file. Depending on your setup try Rscript c:/users/dropbox/Rscripts/CRScore.R Commented Jul 12, 2015 at 19:43
  • Or you can try create CRScore.Rexec and choose Rscript as dafault for Rexec files. Commented Jul 13, 2015 at 8:09
  • @agstudy , The example helped and it worked! Commented Jul 13, 2015 at 9:58

2 Answers 2

2

I was having many issues in getting my R script to run. Gathering from various sources this is what ultimately worked:

  1. Make sure R is installed in a path without spaces in it. My original installation used C:\Program Files\R\R-3.2.0\bin\ to get to Rscript. But this resulted in the error noted here. Installing the latest version of R to C:\R-3.2.1\bin resolved this error.

  2. Make sure any packages you need for the R script file are copied into the new installation's library (e.g., C:\R-3.2.1\library). Mine were in C:\Users\user\Documents\R\win-library\3.2 and my R script couldn't find them when running from the command line.

  3. Create a batch file (.bat). You can use the following code:

    C:\R-3.2.1\bin\Rscript file.R
    exit
    

    Save this file in the same location as your file.R

  4. Run this .bat file in Task Scheduler as an Action by specifying the path to the .bat file in the "Program/script" field (as you have above). Make sure to also specify the location of your file.R in the "Start in (optional)" text field.

Your R script should now be able to run through Task Scheduler.

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

Comments

1

You need to tell scheduler how to access R and then tell it what to execute.

In the window you show above (New Action), this is an example of how it should be set-up:

Program/Script: "C:\Program Files\R\R-3.6.0\bin\Rscript"

Add arguments (optional): -e "source('C:\FileName.R')"

Task Scheduler: Action Window

As a side note, if you want to run an rmarkdown file, you need to instead use this syntax:

-e "library(rmarkdown);Sys.setenv(RSTUDIO_PANDOC='C:/Program Files/RStudio/bin/pandoc'); render('C:/FileName.Rmd')"

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.