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.

Shoaibkhanz
  • 1,942
  • 3
  • 24
  • 41
  • 3
    You should use `Rscript.exe` and put it in a `barch` file. See [this](http://stackoverflow.com/questions/17339438/r-script-from-command-line/17341933#17341933) for example. – agstudy Jul 12 '15 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 – jeremycg Jul 12 '15 at 19:43
  • Or you can try create CRScore.Rexec and choose Rscript as dafault for Rexec files. – Batanichek Jul 13 '15 at 08:09
  • @agstudy , The example helped and it worked! – Shoaibkhanz Jul 13 '15 at 09:58

2 Answers2

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.

Community
  • 1
  • 1
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')"