Function to Simplify Loading and Installing Packages

February 20, 2014

(This article was first published on Blog - R, and kindly contributed to R-bloggers)

One of the more tedious parts of working with R is maintaining my R library. To make my R scripts reproducible and sharable, I will install packages if they are not available. For example, the top of my R scripts tend to look something like this:

if(!require(devtools) | !require(ggplot2) | !require(psych) | !require(lme4) | !require(benchmark)) {

This has worked fine for some time, but I felt there was a better approach. First, note that if any one package doesn’t load (usually because it is not installed), all the packages are installed. I could separate the if statement so there is one per package, but then I have even more lines in my R script. Instead, I have written a function that will load each package separately and install only those that are not present. And optionally will even update packages using the update parameter. For example, I can now replace the above with one call to package:

> package(c('devtools','ggplot2','psych','lme4','benchmark'))

The output is minimal by default (set quiet=FALSE to get all the messages printed by require and install.packages). Even though verbose=TRUE by default, the only messages it will print is to indicate that a newer version of a package is available or that the package is not available on the repositories. In place of output to the console, a data frame is returned with a summary of what packages have been loaded and/or installed along with the loaded and available versions. Here are the results from the command above:

A newer version of lme4 is available (current=1.0.5; available=1.0.6)

          loaded installed loaded.version available.version
devtools    TRUE     FALSE          1.4.1             1.4.1
ggplot2     TRUE     FALSE 
psych       TRUE     FALSE 
lme4        TRUE     FALSE          1.0.5             1.0.6
benchmark   TRUE      TRUE          0.3.5             0.3.5

Note that if I had specified update=TRUE (it is FALSE by default) the lme4 package would have been automatically updated.

In summary, I have collapsed what usually takes several lines within my R scripts to just one line, or two if you need to source this function. However, I just source this function in my .Rprofile so that it is always available. The only potential downside is that this is not part of base R and requires anyone you share your R scripts with to also have this function available.

The sourse code is on Gist here:
With devtools installed you can just source function using:

> source_gist(9112634)

To leave a comment for the author, please follow the link and comment on their blog: Blog - R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training


CRC R books series

Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)