How to Source an R script automatically on a Mac using Automator and iCal

[This article was first published on Pass the ROC, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I wrote an R script that pulled data from an RSS feed.  The RSS feed updated frequently, so I wanted to be able to schedule the script to run automatically.  After some tinkering, I got it to work by implementing the steps below.  Note that these steps assume you do not want to save your workspace – that you are saving the objects you need explicitly within the script. 

Step 3: The Run Shell Script Action
  1. Test your R script in regular ole’ R to make sure it runs without error.
  2. Add a quit(save=”no”) command at the end of your script. 
  3. Open Automator  (Applications -> Automator)
  4. Select Application from the Template Selection menu
  5. Select the Run Shell Script Action, double click or drag it over
  6. Type:  Rscript –no-save –no-restore /Users…./YourScript.R
  • You can test your “Application” now by hitting the Run button in the upper right corner.  Ignore the little robot’s warning.  Hopefully, you’ll see some green checkmarks and a “Workflow completed” message.
  • Save your “Application” somewhere clever.
  • Now, open iCal and create a new event.  Set the date, time, and repeat values as you wish.  Select alarm->Open File.
  • It will default to iCal; click iCal, select Other, and navigate to your “Application,” which you saved somewhere clever.
  • That’s it, you’re done.  When the scheduled time comes, the script will run in the background without even opening R.  You may see a little gear up top on your menu bar, that’s it.  

    To leave a comment for the author, please follow the link and comment on their blog: Pass the ROC.

    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.