You can find thousands of R 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-source 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.)
You can browse through the set of available packages with your web browser. Here are some places to look for packages.
|CRAN||See 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|
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
package is a better choice for this algorithm, but we’ll get to that
(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.
From the Packages menu, select “Install package(s)”.
If this is the first time 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.)
Click the name of the package that you want to install and press OK.
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.
From the Package and Data menu, select Package Installer. (See Figure 4-1 for a picture of the installer window.)
(Optional) In the top-left corner of the window is a menu that allows you to select the category of packages you would like to download. Initially, this is set to “CRAN (binaries).”
Click the Get List button to display the available set of packages.
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.)
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.
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
maptree. You could accomplish this with the
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
> remove.packages(c("tree", "maptree"),.Library)
Table 4-2. Common package installation commands
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
$ 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)