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!
Originally posted on TheAutomatic.net blog.
Disclosure: Interactive Brokers
Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.
This material is from TheAutomatic.net and is being posted with its permission. The views expressed in this material are solely those of the author and/or TheAutomatic.net and Interactive Brokers is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.
Join The Conversation
If you have a general question, it may already be covered in our FAQs. If you have an account-specific question or concern, please reach out to Client Services.