Skip to content
Advertisement

Schedule a function that belongs to an R package

I’m trying to build an R package whose goal is to run a series of analyses by taking input data and writing output data to an external database (PostgreSQL).

Specifically, I need a set of operations to be scheduled to run on a daily basis. Therefore, I have written some bash scripts with R code (using the header #!/usr/bin/env Rscript) and I have saved them into the exec/ folder of the R package. The scripts make several call to the package’s core functions in R/ folder.

At this point, once installed the package on a linux server, how do I set up a crontab that is able to directly access the scripts in the exec/ folder?

Is this way of proceeding correct or is there a different best practice for such operations?

Advertisement

Answer

We do this all the bleeping time at work. Here at home I also have a couple of recurring cronjobs, eg for CRANberries. The exec/ folder you reference works, but my preferred solution is to use, say, inst/scripts/someScript.R.

Then one initial time you need create a softlink from your package library, say, /usr/local/lib/R/site-library/myPackage/scripts/someScript.R to a directory in the $PATH, say /usr/local/bin.

The key aspect is that the softlink persists even as you update the package. So now you are golden. All you now need is your crontab entry referencing someScript.R. We use a mix of Rscript and littler scripts.

Advertisement