5.1 Extending R with packages

One of the reason that R has such a following in the statistical and biological fields is the range of available packages that can be used to extend the utility of the software. Packages are collections of functions, data, and compiled code written in R and other languages that are encapsulated and distributed in the package format. Packages are generally domain or analysis specific, thus are only installed on a as needed basis. The directory where packages are stored is called the library.

R comes with a standard set of packages. Others are available for download and installation from repositories that include CRAN, Bioconductor and R-forge. To use a package, you must first install it and then load it into your session using either the require or library functions.

Because of the configuration of the training server we are using today, you can only install packages to your own personal library and not a system wide installed. This means that the packages you install will not be available for use by another participant in this class.

The code below outlines the process involved:

# Look to see which packages are already installed
library()

# To install one that is not there, use install.packages
# N.B. You will get a message "Would you like to use a personal library instead?"
install.packages("gplots")

# Once it's installed, we need to load it
require("gplots")
# or
library("gplots")

# library() shows the installed packages. search() shows which of those are loaded.
search()

# If you no longer need a package, then you may want to unload it with detach()
# You need to specify "package:" before the name, as per the listing from search()
detach("package:gplots")

Using packages

  1. Use library() and search() to see what packages are available on the training server and loaded into your workspace.