In this entry in a small series of tips for the use of the R statistical analysis and computing tool, we look at how to keep your addon packages up-to-date.
One of the great strengths of R is the many packages available. All the new approaches, as well as some of the best implementations of your old favorites are there. But it can also be a little daunting, and so theCRAN task views are often the best way to get started and download a reasonable “bundle” of packages for your analysis.
First we need a place to store the packages. On Linux (and other Unix-like systems) I use the file~/.Renviron to set the R_LIBS variable to where I want the files:
## R environment
On Windows, I set the same variable for the user account. Don’t forget to create the directory.
Now your can start R and install the CRAN task view package:
Then I have a few things in my ~/.Rprofile startup file. The previous command probably prompted you for a download mirror which is annoying, so let’s exit R and edit the startup file to contain:
Try it out! Save the file, start R, and type install.myviews() at the prompt. If your list is as long as mine, then this may take some time and you may get some warnings and errors. We might add a tip on these later, but the main reason for the errors is probably that you are missing the development files for external libraries (or that R just can’t find it).
Now that we have finally got them, we need to make sure they are up-to-date. I add two functions to~/.Rprofile:
The first allows me to easily update all my locally installed libraries (not just these installed from views). The second updates my views which is useful when the view definitions change (rarely, but it happens as the recommended packages evolve).
Now I can of course update from the R command prompt using update.local() or update.myviews(). But that is not the main benefit. I can now update directly from the shell command line using commands like:
echo "update.local()" > /tmp/r.cmd
R CMD BATCH /tmp/r.cmd /tmp/r.out
The beauty of this is that I can add it to my crontab(5) and have it run automatically every night or every week as I feel I need it. This way I always have the latest versions installed.