How to run R from the Task Scheduler
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
In a prior post, we covered how to run Python from the Task Scheduler on Windows. This article is similar, but it’ll show how to run R from the Task Scheduler, instead. Similar to before, let’s first cover how to R from the command line, as knowing this is useful for running it from the Task Scheduler.
Running R from the Command Line
To open up the command prompt, just press the windows key and search for cmd. When R is installed, it comes with a utility called Rscript. This allows you to run R commands from the command line.
If Rscript is in your PATH, then typing Rscript into the command line, and pressing enter, will not result in an error. Otherwise, you might get a message saying “‘Rscript’ is not recognized as an internal or external command.” If that’s the case, then to use Rscript, you will either need to add it to your PATH (as an environment variable), or append the full directory of the location of Rscript on your machine. To find the full directory, search for where R is installed your computer. For instance, it may be something like below (this will vary depending on what version of R you have installed):
C:\Program Files\R\R-3.4.3
Within this folder, there should be a sub-folder called bin. This sub-folder contains Rscript.
Thus, the full path is:
C:\Program Files\R\R-3.4.3\bin\Rscript
For appending the PATH variable, see this link.
Using the above we can run an R command like this:
“C:\Program Files\R\R-3.4.3\bin\Rscript” -e “cat(‘this is a test’)”
Or – if you have Rscript in your path, you can run this:
Rscript -e “cat(‘this is a test’)”
This code will run the cat command to print out test. The quotes are necessary around the full path to Rscript in the first option because of the spaces in the path name. The -e flag refers to expression. Thus the expression following -e gets executed.
Hence, if we want to run an R script from the command line, we can call source with Rscript. Suppose the name of the script we want to run is called “C:/path/to/script/some_code.R”
Then, we can run this script from the command line like so:
Rscript -e “source(‘C:/path/to/script/some_code.R’)”
Running R from the Task Scheduler
Now, we can take this notion to run R via the Task Scheduler. Pressing the windows key, followed by typing “task scheduler” should bring the Task Scheduler up. Once it’s open, click on “Action”, and then press “Create Task”.
After this, you will see a place where you need to input the name of your task.
You will also see where you can select “Run whether user is logged on or not.” If you need to run a script in production where you won’t necessarily be logged on all the time, then you should select the “whether user is logged on or not” option. As a note, however, you will need to have the appropriate permissions for this to work for you. This generally means you’ll need to have batch job rights. If you’re an administrator on the machine you’re creating the task, then this shouldn’t be a problem.
If you’re just running a task on a computer where you’ll be logged on when you need it to run, then you can select the “Run only when user is logged on” option.
The next step is to select the “Triggers” tab.
You should get a box that pops up where you can select what dates / times you need your script to run. After you’ve made your selections, you can go to the “Actions” tab, where you’ll create a new action. Clicking “New” brings up the box below.
There’s a couple ways you can add an action here. One, you can copy exactly what we did above to run an R script from the command line into the “Program/Script” box:
Rscript -e “source(‘C:/path/to/script/some_code.R’)”
Or two, you can type Rscript into this box, and put the rest of the line, -e “source(‘C:/path/to/script/some_code.R’)” into the “Add Arguments (optional)” box.
Lastly, just press “OK”, and you’re done!
That’s it for this post! Check out other R articles of mine here: http://theautomatic.net/category/r/
The post How to run R from the Task Scheduler appeared first on Open Source Automation.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.