This tutorial shows a few useful functions to deal with packages that you may be unaware of. They are mostly useful when writing scripts that you’d like to be reproducable on another machine.

.libPaths()

When you install.packages, where do they go? To check this, use this function.

.libPaths()
## [1] "c:/RLIBRARY"                       
## [2] "C:/Program Files/R/R-3.1.1/library"

By default, packages will be installed to the first directory listed there.

packageVersion()

Sometimes you want to make sure you use at least a certain version of a package.

packageVersion("gplots")
## [1] '2.14.2'

That lists the package, to check if it is larger than some version, do:

packageVersion("gplots") > "2.13"
## [1] TRUE

So at the top of a script, you may do:

if(packageVersion("gplots") < "2.14")stop("Update gplots first!")

.packages(TRUE)

You may want to check whether a certain package is installed, and if not, install it on the fly. This makes running a script on another machine less painful.

(Here I use head to show only the first few, of course you don’t actually use that!)

head(.packages(TRUE))
## [1] "abind"   "acepack" "ade4"    "adimpro" "ape"     "aplpack"

Using this, we can put a line like this at the top of a script:

if(!"gtools" %in% .packages(TRUE))install.packages("gtools")

citation()

When you use a package in a manuscript, you should really cite the package if you can. Use this function to figure out how.

citation("mgcv")
## 
## 2011 for generalized additive model method; 2004 for strictly
## additive GCV based model method and basics of gamm; 2006 for
## overview; 2003 for thin plate regression splines; 2000 is the
## original method, now superceded.
## 
##   Wood, S.N. (2011) Fast stable restricted maximum likelihood and
##   marginal likelihood estimation of semiparametric generalized
##   linear models. Journal of the Royal Statistical Society (B)
##   73(1):3-36
## 
##   Wood, S.N. (2004) Stable and efficient multiple smoothing
##   parameter estimation for generalized additive models.Journal of
##   the American Statistical Association. 99:673-686.
## 
##   Wood, S.N. (2006) Generalized Additive Models: An Introduction
##   with R. Chapman and Hall/CRC.
## 
##   Wood, S.N. (2003) Thin-plate regression splines. Journal of the
##   Royal Statistical Society (B) 65(1):95-114.
## 
##   Wood, S.N. (2000) Modelling and smoothing parameter estimation
##   with multiple quadratic penalties. Journal of the Royal
##   Statistical Society (B) 62(2):413-428.

update.packages(ask=FALSE)

You should update all your packages pretty often (every few weeks), to avoid nasty surprises when a newer version breaks your code.

update.packages(ask=FALSE)

require() vs. library()

These two functions seem to be identical,

require(plotrix)
## Loading required package: plotrix
library(plotrix)

but require invisibly returns a TRUE or FALSE, indicating whether the load was successful or not. This is useful inside a function, where you try to load a package, and if it fails you stop.

r <- require(plotrix)
r
## [1] TRUE
r <- require(madeuppackage)
## Loading required package: madeuppackage
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'madeuppackage'
r
## [1] FALSE