Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R can't find some packages when running via crontab

I am having problems when trying to run my R script using Rscript via crontab.

The following command works fine when running in the command line

Rscript /var/www/html/sent/sentiment/code/parse.r

But the following line inside crontab

*/5 * * * * Rscript /var/www/html/sent/sentiment/code/parse.r > /var/www/html/sent/sentiment/code/backup.log 2>&1

Will return the following error in the log

Error in library(twitteR) : there is no package called 'twitteR'
Execution halted

Why is it possible that Rscript won't be able to find the packages when running using cron? How can I make crontab 'see' my R packages.

Any tip much appreciated.

like image 495
JordanBelf Avatar asked Feb 10 '13 01:02

JordanBelf


2 Answers

As suggested in the comments, the problem might be that you and your crontab are using a different R install.

To check if it is the case, run which Rscript as yourself and as crontab.

If they are different (which I suspect), you could use the full path to the appropriate Rscript when you are calling it from crontab. A more permanent solution would require setting environment variables.

like image 82
flodel Avatar answered Sep 20 '22 02:09

flodel


It will most likely be your .libPath() setting -- which is why I instrument to the Debian/Ubuntu package to use a system-wide, rather than per-user, setting.

To check, run as you as well as from the same crontab

 print(.libPaths())
 print(installed.packages())

The difference should become clear. I think I answered an almost identical question here before.

like image 37
Dirk Eddelbuettel Avatar answered Sep 22 '22 02:09

Dirk Eddelbuettel