Using R — Installing Packages

This entry is part 2 of 22 in the series Using R

One of the reasons to use R for analysis and visualization is the rich ecosystem of ‘packages’ contributed by others. In most cases, just as with smartphones, “There’s a package for that.” If you want to be efficient you need to embrace other people’s work and in the case of R that means installing packages.  This post walks you through the basics of package installation and use and gives some tips on workarounds when a package won’t install.

Simple example

For the impatient lets start off with a simple example. In this example (on Ubuntu Linux) we’ll run R as the superuser so that packages will be installed in the default location. We will install the “geonames” package and then show off the new functionality we just added.

$ sudo R --vanilla
> install.packages(c("geonames"))
Warning in install.packages(c("geonames")) :
  argument 'lib' is missing: using '/usr/local/lib/R/site-library'
--- Please select a CRAN mirror for use in this session ---
Loading Tcl/Tk interface ...
* DONE (geonames)

The downloaded packages are in

If you don’t run R as superuser you won’t have permission to write packages into the site-library and you will be prompted to create a personal library. You can specify the library, repository and a few other options by passing parameters to the install.packages() method. Use ?install.packages to learn more.

So what extra functionality does this new “geonames” package bring?  You’ll have to do a little reading to figure out the details but for now just paste these lines into your R session:

# first load the new package

# now define a function to convert zip codes into weather reports
zip_weather <- function(postalcode) {
 options(warn=-1) # Ignore warnings about 'incomplete final line'.
 zip = GNfindNearbyPostalCodes(postalcode=postalcode,country='US',
 weather = GNfindNearByWeather(zip$lat,zip$lng)
 tempF = as.numeric(weather$temperature) * 9/5 + 32
 line1 = paste(weather$datetime,' GMT',sep='')
 line2 = paste('Weather for ',weather$stationName,sep='')
 line3 = paste('temp = ',tempF,' deg. F with ',weather$clouds,
 ', wind at ',weather$windSpeed,' knots',sep='')
 options(warn=0) # Restore default warnings.

Here is the result:

> zip_weather(20066)
[1] "2011-05-12 17:52:00 GMT"
[1] "Weather for Washington DC, Reagan National Airport"
[1] "temp = 71.06 deg. F with few clouds, wind at 11 knots"
> zip_weather(63146)
[1] "2011-05-12 16:51:00 GMT"
[1] "Weather for St. Louis, Lambert-St. Louis International Airport"
[1] "temp = 80.96 deg. F with few clouds, wind at 12 knots"
> zip_weather(98107)
[1] "2011-05-12 17:53:00 GMT"
[1] "Weather for Seattle, Seattle Boeing Field"
[1] "temp = 53.06 deg. F with few clouds, wind at 06 knots"

Who’d-a-thunk that R could so easily be turned into a real time weather system?


In order to install and make use of packages you first have to find them.  Luckily, most (but not all) R packages are organized and available from CRAN — the Comprehensive R Archive Network. Just click on the Packages link to see the full list of contributed packages. Packages are listed alphabetically with a short description. Unfortunately, there is no rating system but you can get a quick sense of quality by clicking on a package link and looking at the “Published” date and especially any “Reverse dependencies” listed at the the bottom of a package. Reading the documentation and looking at the number of releases in the “Old sources” is also very helpful.

CRAN also maintains a set of Task Views that identify all the packages associated with a particular task. The maintainers of these views do a generally excellent job of staying on top of their area of interest and giving a detailed summary of which packages do what. If one of the task views is a perfect match you can have R install every package from that view using the “ctv” package. Yes, “ctv” is a package to automate package installation. See the section below on “Installing older versions” if you have trouble installing “ctv”.

Installing packages

The basics of package installation are given in chapter 6 of R Installation and Administration. There are two ways to do a command line installation of packages: from the R command line and from the shell command line.

> install.packages() # at the R prompt

Within R you can use install.packages() as demonstrated in the example above. This will always attempt to install the latest version of packages it knows about.

$ R CMD INSTALL # at the shell prompt

You can also invoke R from the command line. This is useful for some packages when install.packages() doesn’t work or for packages that are not part of CRAN. More information is available with R CMD INSTALL --help. To install packages this way you must first download the package source to your local machine. Here is a quick demonstration:

$ # download the "fortunes" package using 'wget'
$ wget
$ # install the package as superuser
$ sudo R CMD INSTALL fortunes_1.4-1.tar.gz
* DONE (fortunes)

Installing older versions

If you have total control over your system and always keep it at the bleeding edge then you will have no problem installing the latest and greatest versions of R packages. However, if your version of R is older (Perhaps you are running R on a webserver with CentOS?) then some of the more recent releases of packages will not work and install.packages() will generate messages like:

Warning message:
In install.packages(c("sp")) : package ‘sp’ is not available

This is when you have to poke around in the “Old sources” link on the CRAN page for that package and use trial-and-error to find an older version of the package that will work with your version of R.  You should start by determining what version of R you have:

$ R --version
R version 2.8.1 (2008-12-22)

Given that our version of R was released at the end of 2008, any version of the “sp” package released in 2008 should definitely work. At least some of the 2009 releases should also work. Perusing the sp archive, we might try installing version 0.9-37, the last of the 0.9-3x series which was released in May of 2009:

$ wget
$ sudo CMD INSTALL sp_0.9-37.tar.gz
$ # Success!

Using packages

To use a package you start up R and load packages one at a time with the library() command.

> # load a specific package
> library(package_name)
> # learn about a package
> ?package_name
> # find out what packages are installed
> library()
Packages in library '/usr/local/lib/R/site-library':

Over time, your package library will contain more and more packages. Or  perhaps system administrators or other users have also installed packages. It’s good to know what’s installed and at what version. This is where the location of the package library comes in handy. If you poke around you will find out that most packages come with a DESCRIPTION file that contains that information. To see all the package versions on our Ubuntu system we could just type:

$ head -4 /usr/local/lib/R/site-library/*/DESCRIPTION
==> /usr/local/lib/R/site-library/abind/DESCRIPTION <==
Package: abind
Version: 1.1-0
Date: 2004-03-12
Title: Combine multi-dimensional arrays

Of course there is also an ‘R’ way of getting this information. All of the fields in DESCRIPTION files are accessible through the installed.packages() command (note the spelling) which returns a matrix of information with packages as row names and fields as column names. The following example shows how to access this information programmatically from within R:

> packages = installed.packages()
> rownames(packages) # to see all installed packages
  [1] "abind"              "ade4"               "adegenet"
> colnames(packages) # to see all fields
  [1] "Package"   "LibPath"   "Version"   "Priority"  "Depends"   "Imports"
  [7] "LinkingTo" "Suggests"  "Enhances"  "OS_type"   "License"   "Archs"  
  [13] "Built"  
> packages["sp",c("Package","Version","License")] 
    Package      Version      License  
       "sp"     "0.9-72" "GPL (>= 2)"

Special Cases


The ncdf package requires that NetCDF — including the development libraries — first be installed on your system. Unfortunately, the NetCDF libraries and include files are not installed in a uniform location across Unix systems. This is a case where we need to pass configuration arguments to R CMD INSTALL. Here is what ended up working on Ubuntu 10.04 LTS:

sudo R CMD INSTALL --configure-args="--with-netcdf-include=/usr/include --with-netcdf-lib=/usr/lib" ncdf_1.6.5.tar.gz

And for anyone stuck on CentOS 5.x:

R CMD INSTALL --configure-args="--with-netcdf-include=/usr/include/netcdf-3 --with-netcdf-lib=/usr/lib" ncdf_1.6.5.tar.gz

Here’s an August, 2013 update for CentOS 6.x:

sudo wget
sudo R CMD INSTALL --configure-args="--with-netcdf-include=/usr/include --with-netcdf-lib=/usr/lib" ncdf_1.6.6.tar.gz



Not every package will install automatically.  For error messages and workarounds please see the post on Package Installation Problems.

Series NavigationUsing R — DocumentationUsing R — A Script Introduction to R
This entry was posted in R and tagged , , . Bookmark the permalink.

Comments are closed.