120

I have written an R script that pulls some data from a database, performs several operations on it and post the output to a new database.

I would like this script to run every day at a specific time but I can not find any way to do this effectively.

Can anyone recommend a resource I could look at to solve this issue? I am running this script on a Windows machine.

petermeissner
  • 10,587
  • 5
  • 54
  • 58
ETD
  • 1,201
  • 2
  • 9
  • 3

7 Answers7

115

Actually under Windows you do not even have to create a batch file first to use the Scheduler.

  • Open the scheduler: START -> All Programs -> Accesories -> System Tools -> Scheduler
  • Create a new Task
  • under tab Action, create a new action
  • choose Start Program
  • browse to Rscript.exe which should be placed e.g. here:
    "C:\Program Files\R\R-3.0.2\bin\x64\Rscript.exe"
  • input the name of your file in the parameters field
  • input the path where the script is to be found in the Start in field
  • go to the Triggers tab
  • create new trigger
  • choose that task should be done each day, month, ... repeated several times, or whatever you like
petermeissner
  • 10,587
  • 5
  • 54
  • 58
  • 3
    Yes, thank you! This is what worked for me, not the above and not the various other answers floating around the web. I would only add that make sure the file extension is `.R` and NOT `.r`. – esa606 Jul 09 '14 at 21:05
  • What exactly do you mean by that - what would you expect to happen? Maybe this should go into an question of its own (maybe there is an answer already on SO). – petermeissner Jul 15 '16 at 09:10
  • 1
    Is there a way the dos window be minimized? – George Dontas Feb 14 '19 at 15:10
60

Supposing your R script is mytest.r, located in D:\mydocuments\, you can create a batch file including the following command:

C:\R\R-2.10.1\bin\Rcmd.exe BATCH D:\mydocuments\mytest.r

Then add it, as a new task, to windows task scheduler, setting there the triggering conditions.

You could also omit the batch file. Set C:\R\R-2.10.1\bin\Rcmd.exe in the program/script textbox in task scheduler, and give as Arguments the rest of the initial command: BATCH D:\mydocuments\mytest.r

Scheduling R Tasks via Windows Task Scheduler (Posted on February 11, 2015)

taskscheduleR: R package to schedule R scripts with the Windows task manager (Posted on March 17, 2016)

EDIT

I recently adopted the use of batch files again, because I wanted the cmd window to be minimized (I couldn't find another way).

Specifically, I fill the windows task scheduler Actions tab as follows:

Program/script:

cmd.exe

Add arguments (optional):

/c start /min D:\mydocuments\mytest.bat ^& exit

Contents of mytest.bat:

C:\R\R-3.5.2\bin\x64\Rscript.exe D:\mydocuments\mytest.r params

George Dontas
  • 27,579
  • 17
  • 103
  • 140
10

Now there is built in option in RStudio to do this, to run scheduler first install below packages

  install.packages('data.table')
  install.packages('knitr')
  install.packages('miniUI')
  install.packages('shiny')
  install.packages("taskscheduleR", repos = "http://www.datatailor.be/rcube", type = 
  "source")

After installing go to

**TOOLS -> ADDINS ->BROWSE ADDINS ->taskscheduleR -> Select it and execute it.**

enter image description here

Zeeshan
  • 1,045
  • 9
  • 21
  • This doesn't work for me. When I try to run the Addin, I get the following errors: Loading required namespace: shiny Failed with error: ‘number of columns of matrices must match (see arg 2)’ Loading required namespace: miniUI Failed with error: ‘number of columns of matrices must match (see arg 2)’ Error in rbind(info, getNamespaceInfo(env, "S3methods")) : number of columns of matrices must match (see arg 2) – obewanjacobi Aug 13 '19 at 15:48
  • Just updated data.table to try and fix this error, and now I get the following: Error: object 'as.xts' not found whilst loading namespace 'data.table' – obewanjacobi Aug 13 '19 at 15:52
  • 3
    Does RStudio need to remain open for these to work? – cgage1 Oct 02 '19 at 15:03
  • 2
    I think if R studio is close it will automatically open it and run the script. – Zeeshan Oct 03 '19 at 09:12
  • This is the only answer that worked for me. I think the options using task scheduler in Windows couldn't find my libraries. – Nova Nov 25 '20 at 17:40
  • 1
    No actually it won't open R, it only opens a cmd window and executes the R script saved in the "Rscript repo" location (that you set above) in the background. An extra note for this option: the default running directory is in the system folder, just use `setwd` to get the directories right. Using `here` package won't work in this case. – Yang Liu Mar 03 '21 at 11:22
7

Setting up the task scheduler

Step 1) Open the task scheduler (Start > search Task Scheduler)

Step 2) Click "Action" > "Create Task"

Step 3) Select "Run only when the user is logged on", uncheck "Run with highest priveledges", name your task, configure for "Windows Vista/Windows Server 2008"

enter image description here

Step 4) Under the "Triggers" tab, set when you would like the script to run

Step 5) Under the "Actions" tab, put the full location of the Rscript.exe file, i.e.

"C:\Program Files\R\R-3.6.2\bin\Rscript.exe" (include the quotes)

Put the name of your script with with -e and source() in arguments wrapping it like this:

-e "source('C:/location_of_my_script/test.R')"

enter image description here

Troubleshooting a Rscript scheduled in the Task Scheduler

When you run a script using the Task Scheduler, it is difficult to troubleshoot any issues because you don't get any error messages.

This can be resolved by using the sink() function in R which will allow you to output all error messages to a file that you specify. Here is how you can do this:

# Set up error log ------------------------------------------------------------
error_log <- file("C:/location_of_my_script/error_log.Rout", open="wt")
sink(error_log, type="message")

try({

# insert your code here

})

The other thing that you will have to change to make your Rscript work is to specify the full file path of any file paths in your script.

This will not work in task scheduler:

source("./functions/import_function.R")

You will need to specify the full file path of any scripts you are sourcing within your Rscript:

source("C:/location_of_my_script/functions/import_function.R")

Additionally, I would remove any special characters from any file paths that you are referencing in your R script. For example:

df <- fread("C:/location_of_my_data/file#2342.csv")

may not run. Instead, try:

df <- fread("C:/location_of_my_data/file_2342.csv")
ben.watson.ca
  • 91
  • 2
  • 6
4

I set up my tasks via the SCHTASKS program. For running scripts on startup, you would write something along the lines of

SCHTASKS /Create /SC ONSTART /TN MyProgram /TR "R CMD BATCH --vanilla d:\path\to\script.R"

See this website for more details on SCHTASKS. More details at Microsoft's website.

Roman Luštrik
  • 64,404
  • 24
  • 143
  • 187
3

You can use Windows Task Scheduler.

Mark Byers
  • 719,658
  • 164
  • 1,497
  • 1,412
1

After following any combination of these steps and you receive the "Argument Batch Ignored" error after R.exe runs, try this, it worked for me.

In Windows Task Scheduler:

Replace BATCH "C:\Users\desktop\yourscript.R"in the arguments field

with

CMD BATCH --vanilla --slave "C:\Users\desktop\yourscript.R"

Starbucks
  • 1,073
  • 2
  • 14
  • 27
  • 1
    This would make sense as a comment on Mark Byers' answer (or maybe an edit to that answer), I don't think it can stand alone. – Gregor Thomas Aug 07 '17 at 20:56