3

I like that R updates frequently to give me the best new packages, but it does seem to pose a problem for my R scripts on cron schedules.

I noticed that every time ubuntu updater updates my r-base package, when I try to run my scripts, it'll say something like "there is no library(XML)".

Then I have to go "update.packages("XML")" to get the new version of that package that is compatible with my new version of R.

I have a lot of packages installed, so I find myself fairly regularly tracking them down and making long commands like "update.packages(c("XML", "ggplot2", "timeseries"))".

When I forget to do this after an update, all my R cron scripts fail.

Does anyone have a good workflow for automatically checking which packages I have already installed, and updating them when they need to be updated?

1 Answer 1

4

You could add an install.packages() call in your cron script, so that it installs all current packages before it runs the rest of your R cron jobs.

install.packages takes a vector, so you could even keep the file with the packages you want installed elsewhere and then load it automatically in your cron update script.

installed.packages() returns a vector of already-installed packages, so you can use %in% to just call it on packages not yet installed:

desired.packages <- c("plyr","taRifx","ggplot2")
missing.packages <- ! desired.packages %in% installed.packages()[,1]
if( any(missing.packages) ) { 
   install.packages( desired.packages[missing.packages] )
}

Edit: Possibly better solution

As per @JoshuaUlrich's suggestion in chat:

Place this in your .Rprofile.

local({
  .libPaths("/home/evilgenius/R/library")
})

Then you should be able to just run update.packages() after Ubuntu wipes everything, and the monarchy will be restored.

This solution requires greater caution that packages that don't work with the current version of R aren't carried along.

2
  • Yeah, thanks, that will likely do it. Also, Josh Ulrich remains a very helpful super wizard. May 31, 2012 at 18:49
  • 1
    @Mittenchops Indeed. Between hadley() and JU(), my problems solve themselves these days. :-O May 31, 2012 at 19:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .