<p>2 new R packages were put on CRAN last week by BNOSAC (<a href="http://www.bnosac.be/">www.bnosac.be</a>).</p>
<ul><li>One package for scheduling R scripts and processes on Windows (<a href="https://cran.r-project.org/package=taskscheduleR">taskscheduleR</a>) and</li>
<li>Another package for scheduling R scripts and processes on Unix / Linux (<a href="https://cran.r-project.org/package=cronR">cronR</a>)</li>
</ul><p>These 2 packages allow you to <strong>schedule R processes from R directly</strong>. This is done by passing commands directly to <a href="https://en.wikipedia.org/wiki/Cron">cron</a> which is a basic Linux/Unix job scheduling utility or by using the Windows Task Scheduler. The packages were developed for beginning R users who are unaware of that fact that R scripts can also be run non-interactively and can be <strong>automated</strong>.</p>
<p>We blogged already about the taskscheduleR R package at <a href="http://www.bnosac.be/index.php/blog/51-new-rstudio-add-in-to-schedule-r-scripts">this blog post </a>and also <a href="http://www.bnosac.be/index.php/blog/50-taskscheduler-r-package-to-schedule-r-scripts-with-the-windows-task-manager-2">here</a>. This time we devote some more details to the cronR R package.</p>
<p>The <strong>cronR</strong> package allows to</p>
<ul><li><span>Get the list of scheduled jobs</span></li>
<li><span>Remove scheduled jobs</span></li>
<li><span>Add a job</span>
<ul><li>a job is basically a script with R code which is run through Rscript</li>
<li>You can schedule tasks 'ONCE', 'EVERY MINUTE', 'EVERY HOUR', 'EVERY DAY', 'EVERY WEEK', 'EVERY MONTH' or any complex schedule</li>
<li>The task <span>log</span> contains the stdout &amp; stderr of the Rscript which was run on that timepoint. This log can be found at the same folder as the R script</li>
</ul></li>
</ul><p>The package is especially suited for persons working on an RStudio server in the cloud or within the premises of their corporate environment. It allows to easily schedule processes. To make that extremely easy for beginning R users, an <strong>RStudio addin</strong> was developed, which is shown in the example below. The RStudio addin basically allows you to select an R script and schedule it at specific timepoints. It does this by copying the script to your launch/log folder and setting up a cronjob for that script.</p>
<p>The example below shows how to set up a cron job using the RStudio addin so that the scripts are launched every minute or every day at a specific hour. The R code is launched through Rscript and the log will contain the errors and the warnings in case your script failed so that you can review where the code failed.</p>
<p>Mark that you can also pass on arguments to the R script so that you can launch the same script for productXYZ and productABC.</p>
<p></p>
<p>Of course scheduling scripts can also be done from R directly. Some examples are shown below. More information at <a href="https://github.com/bnosac/cronR">https://github.com/bnosac/cronR</a></p>
<div>
<div>
<pre>library(cronR)<br>f &lt;- system.file(package = &quot;cronR&quot;, &quot;extdata&quot;, &quot;helloworld.R&quot;)<br>cmd &lt;- cron_rscript(f, rscript_args = c(&quot;productx&quot;, &quot;20160101&quot;))<br>## Every minute<br>cron_add(cmd, frequency = 'minutely', id = 'job1', description = 'Customers')<br>## Every hour at 20 past the hour on Monday and Tuesday<br>cron_add(cmd, frequency = 'hourly', id = 'job2', at = '00:20', description = 'Weather', days_of_week = c(1, 2))<br>## Every day at 14h20 on Sunday, Wednesday and Friday<br>cron_add(cmd, frequency = 'daily', id = 'job3', at = '14:20', days_of_week = c(0, 3, 5))<br>## Every starting day of the month at 10h30<br>cron_add(cmd, frequency = 'monthly', id = 'job4', at = '10:30', days_of_month = 'first', days_of_week = '*')<br>## Get all the jobs<br>cron_ls()<br>## Remove all scheduled jobs<br>cron_clear(ask=FALSE)
</pre>
</div>
</div>
<p>We hope this will gain you some precious time and if you need more help on automating R processes, feel free to <a href="http://www.bnosac.be/index.php/contact/get-in-touch?id=0">get into contact</a>. We have a special training devoted to managing R processes which can be given in your organisation. More information at <a href="http://www.bnosac.be/index.php/training">our training curriculum</a>.</p>
<p></p>
<p> </p>