问题
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.
回答1:
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
回答2:
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
回答3:
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.
回答4:
You can use Windows Task Scheduler.
回答5:
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.**
回答6:
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"
来源:https://stackoverflow.com/questions/2793389/scheduling-r-script