Exploring Package Repositories

The packages included with R are very useful; many users will never need to use any other features. However, you can find thousands of additional packages online.

The two biggest sources of packages are CRAN (Comprehensive R Archive Network) and Bioconductor, but some packages are available elsewhere. (If you know Perl, you’ll notice that CRAN is very similar to CPAN: the Comprehensive Perl Archive Network.) CRAN is hosted by the R Foundation (the same nonprofit organization that oversees R development). The archive contains a very large number of packages (there were 1,698 packages on February 24, 2009), covering a wide number of different applications. CRAN is hosted on a set of mirror sites around the world. Try to pick an archive site near you: you’ll minimize download times and help reduce the server load on the R Foundation.

Bioconductor is an open isource project for building tools to analyze genomic data. Bioconductor tools are built using R and are distributed as R packages. The Bioconductor packages are distributed separately from R, and most are not available on CRAN. There are dozens of different packages available directly through the Bioconductor project.

R-Forge is another interesting place to look for packages. The R-Forge site contains projects that are in progress, and it provides tools for developers to collaborate. You may find some interesting packages on this site, but please be sure to read the disclaimers and documentation because many of these packages are works in progress.

R includes the ability to download and install packages from other repositories. However, I don’t know of other public repositories for R packages. Most R projects simply use CRAN to host their packages. (I’ve even seen some books that use CRAN to distribute sample code and sample data.)

Exploring Packages on the Web

R provides good tools for installing packages within the GUI but doesn’t provide a good way to find a specific package. Luckily, it’s pretty easy to find a package on the Web.

You can browse through the set of available packages with your web browser. Here are some places to look for packages.

RepositoryURL
CRANSee http://cran.r-project.org/web/packages/ for an authoritative list, but you should try to find your local mirror and use that site instead
Bioconductorhttp://www.bioconductor.org/packages/release/Software.html
R-Forgehttp://r-forge.r-project.org/

However, you can also try to find packages with a search engine. I’ve had good luck finding packages by using Google to search for “R package” plus the name of the application. For example, searching for “R package multivariate additive regression splines” can help you find the mda package, which contains the mars function. (Of course, I discovered later that the earth package is a better choice for this algorithm, but we’ll get to that later.)

Finding and Installing Packages Inside R

Once you figure out what package you want to install, the easiest way to do it is inside R.

Windows and Linux GUIs

Installing packages through the Windows GUI is pretty straightforward.

  1. (Optional). By default, R is set to fetch packages from the “CRAN” and “CRAN (extra)” categories. To pick additional sets of packages, choose “Select repositories. . .” from the Packages menu. You can choose multiple repositories.

  2. From the Packages menu, select “Install package(s). . . .”

  3. If this is the first time that you are installing a package during this session, R will ask you to pick a mirror. (You’ll probably want to pick a site that is geographically close, because it’s likely to also be close on the Internet, and thus fast.)

  4. Click the name of the package that you want to install and press the “OK” button.

R will download and install the packages that you have selected.

Note that you may run into issues installing packages, depending on the permissions assigned to your user account. If you are using Windows XP, and your account is a member of the Administrators group, you should have no problems. If you are using Windows Vista, and you installed R in your own directory, you should have no issues. Otherwise, you may need to run R as an Administrator in order to install supplementary packages.

Mac OS X GUI

On Mac OS X, there is a slightly different user interface for package installation. It shows a little more information than the Windows version, but it’s a little more confusing to use.

  1. From the Package and Data menu, select “Package Installer.” (See Figure 4-1 for a picture of the installer window.)

  2. (Optional) In the top lefthand corner of the window is a menu that allows you to select the category of packages that you would like to download. Initially, this is set to “CRAN (binaries).”

  3. Click the “Get List” button to display the available set of packages.

  4. You can use the search box to filter the list to show only packages that match the name you are looking for. (Note: you have to click the “Get List” button before the search will return results.)

  5. Select the set of packages that you want to install and press the “Install Selected” button.

By default, R will install packages at the system level, making them available to all users. If you do not have the appropriate permissions to install packages globally, or if you would like to install them elsewhere, then select an alternative location. Additionally, R will not install the additional packages on which your packages depend. You will get an error if you try to load a package and have not installed other packages on which it is dependent.

R console

You can also install R packages directly from the R console. Table 4-2 shows the set of commands for installing packages from the console. As a simple example, suppose that you wanted to install the packages tree and maptree. You could accomplish this with the following command:

> install.packages(c("tree","maptree"))
trying URL 'http://cran.cnr.Berkeley.edu/bin/macosx/universal/contrib/
2.9/tree_1.0-26.tgz'
Content type 'application/x-gzip' length 103712 bytes (101 Kb)
opened URL
==================================================
downloaded 101 Kb

trying URL 'http://cran.cnr.Berkeley.edu/bin/macosx/universal/contrib/
2.9/maptree_1.4-5.tgz'
Content type 'application/x-gzip' length 101577 bytes (99 Kb)
opened URL
==================================================
downloaded 99 Kb


The downloaded packages are in
     /var/folders/gj/gj60srEiEVq4hTWB5lvMak+++TM/-Tmp-//RtmpIXUWDu/
downloaded_packages

This will install the packages to the default library specified by the variable .Library. If you’d like to remove these packages after you’re done, you can use remove.packages. You need to specify the library where the packages were installed:

> remove.packages(c("tree", "maptree"),.Library)

Table 4-2. Common package installation commands

CommandDescription
installed.packagesReturns a matrix with information about all currently installed packages.
available.packagesReturns a matrix of all packages available on the repository.
old.packagesReturns a matrix of all currently installed packages for which newer versions are available.
new.packagesReturns a matrix showing all currently uninstalled packages available from the package repositories.
download.packagesDownloads a set of packages to a local directory.
install.packagesInstalls a set of packages from the repository.
remove.packagesRemoves a set of installed packages.
update.packagesUpdates installed packages to the latest versions.
setRepositoriesSets the current list of package repositories.

Installing from the command line

You can also install downloaded packages from the command line. (There is actually a set of different commands that you can issue to R directly from the command line, without launching the full R shell.) To do this, you run R with the “CMD INSTALL” option. For example, suppose that you had downloaded the package aplpack (“Another Plotting PACKage”). For Mac OS X, the binary file is called aplpack_1.1.1.tgz. To install this package, change to the directory where the package is located and issue the following command:

R CMD INSTALL aplpack_1.1.1.tgz

If successful, you’ll see a message like the following:

* Installing to library '/Library/Frameworks/R.framework/Resources/library'
* Installing *binary* package 'aplpack' ...
* DONE (aplpack)

Get R in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.