R Programming/Packages

An R package includes a set of functions and datasets. Packages are often developed as supplementary material to books. For instance the MASS package was developed by Venables and Ripley for their book Modern Applied Statistics with S and the car package was developed by John Fox for his book An R and S plus Companion to Applied Regression.

Load a package
A package is loaded into the current R environment using the  function. A list of functions and datasets included in a package can be obtained by using the h or help argument of the library function.

A package can be detached from the current environment by using the  function:

Without any arguments the  function lists all of the packages currently available to the user. (gdata) describe all loaded environments (ie packages). gives the list of all loaded packages.

(Zelig) show all the required and suggested packages.

Where are my packages stored?
 * The  function without arguments prints the library directories
 * The  function with a directory as argument defines a new directory where to store new libraries.

Install new packages

 * Each major distribution of R includes a 'base' set of packages which support many basic statistical functions.
 * Many R Users also choose to install additional 'Add-on' packages to provide simplified interfaces to R commands or to add specialist functionality i.e. the ggplot Grammar of Graphics package provides an advanced graphical output capability.
 * The exhaustive list of all available packages is on the CRAN website.
 * The R community has developed a vast resource of Add-on packages, some with unique functionality, some with overlapping functionality. It is therefore common to find multiple R packages capable of completing the same task i.e. reading and writing Excel spreadsheets. Ultimately which package to use is your choice.
 * To install a new package, it is usually necessary to specify the name of the package as an argument of install.packages function.
 * Sometimes you need to specify more options. For instance, this is the case if you are not an administrator of your computer.
 * "lib" specifies the directory where you want to store the package.
 * "repos" specifies a list of repositories. Note that you can specify a vector of repositories.
 * "dep=T" specifies that all the required packages are also downloaded and installed.


 * Stay up to date.

If you want to be aware of the latest packages, type new.packages in R or visit the Revolution Computing Blog which gives each month a list of the new and the updated packages.

We can also install bundles of packages using  or   (ctv).

We can also remove packages with.

Package Documentation and Help
All R packages install with 'help' documentation, listing their functions and providing syntax and usage examples.

See the Obtaining Help Documentation section for more details on accessing package 'help' documentation.

Package Dependencies

 * Most R packages have dependencies or references to other R packages. You must have all of an R package's 'required' dependencies installed, before you can use the package.
 * R package dependencies come in two types, required and suggested.
 * Specialist R packages such as the ggplot Grammar of Graphics packages have large package dependency trees.
 * The  function will automatically download and install a package and its dependencies, on a computer with an Internet connection.
 * The R CMD INSTALL utility will check preinstalled packages for dependencies, but not download missing packages.
 * Users must follow separate package download and installation processes when working on a computer with no Internet connection. The miniCRAN package can be used to assist in the offline management of R package dependencies.

Building R Packages
You can write down your own R packages. But, all packages submitted to CRAN (or Bioconductor) must follow specific guidelines, including the folder structure of the package and the other files like DESCRIPTION, NAMESPACE and so on.


 * See Friedrich Leisch's introduction (PDF 20 pages)
 * See also Duncan Murdoch's tools for building packages using Windows
 * See also Hadley Wickham and Jennifer Bryan's online book on current packaging practices (R Packages)