R Tutorials Top Banner

PACKAGE MANAGEMENT


Sooner or later, you will probably want to modify or update your R installation. Updating is simple. When a new version of R is released (about every six months), just go to the R Project website, download it, and install it as before. On a Windows machine, the new version will be added to your R program folder, and a new shortcut icon will be placed on your desktop. You will then be able to run either version of R by clicking the appropriate shortcut. (I currenly have six different versions of R on my Windows machine in my office. Don't ask me why. I don't have a good excuse for it.) On a Mac, the new version will replace the old version in the Applications folder, and the icon in your Dock will open the new version. In Linux I'm not sure what happens. I use the package manager to do updates in Linux.

There is also an enormous number of extension packages for R that will add new functionality. Before I get to those, let's see how you can tell what you've already got. First, let's look at the search path again...

> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"
The search( ) function shows you the packages that are started up by default when you start R, and the order in which they are searched for commands you issue or data objects you request. (The ".GlobalEnv" is your workspace.) This is not a complete list of the installed packages, however. To see that, do this...
> installed.packages()
           Package      LibPath                          Version  
base       "base"       "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
boot       "boot"       "C:/PROGRA~1/R/R-27~1.1/library" "1.2-33" 
class      "class"      "C:/PROGRA~1/R/R-27~1.1/library" "7.2-42" 
cluster    "cluster"    "C:/PROGRA~1/R/R-27~1.1/library" "1.11.11"
codetools  "codetools"  "C:/PROGRA~1/R/R-27~1.1/library" "0.2-1"  
datasets   "datasets"   "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
foreign    "foreign"    "C:/PROGRA~1/R/R-27~1.1/library" "0.8-26" 
graphics   "graphics"   "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
grDevices  "grDevices"  "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
grid       "grid"       "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
KernSmooth "KernSmooth" "C:/PROGRA~1/R/R-27~1.1/library" "2.22-22"
lattice    "lattice"    "C:/PROGRA~1/R/R-27~1.1/library" "0.17-8" 
MASS       "MASS"       "C:/PROGRA~1/R/R-27~1.1/library" "7.2-42" 
methods    "methods"    "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
mgcv       "mgcv"       "C:/PROGRA~1/R/R-27~1.1/library" "1.4-0"  
nlme       "nlme"       "C:/PROGRA~1/R/R-27~1.1/library" "3.1-89" 
nnet       "nnet"       "C:/PROGRA~1/R/R-27~1.1/library" "7.2-42" 
rpart      "rpart"      "C:/PROGRA~1/R/R-27~1.1/library" "3.1-41" 
spatial    "spatial"    "C:/PROGRA~1/R/R-27~1.1/library" "7.2-42" 
splines    "splines"    "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
stats      "stats"      "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
stats4     "stats4"     "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
survival   "survival"   "C:/PROGRA~1/R/R-27~1.1/library" "2.34-1" 
tcltk      "tcltk"      "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
tools      "tools"      "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"  
utils      "utils"      "C:/PROGRA~1/R/R-27~1.1/library" "2.7.1"
And there is more output than that, but I have not shown all of it. (And it may be different under different OSes.) If you want information on one of these packages, you can get it this way...
> packageDescription("MASS")
Bundle: VR
Priority: recommended
Contains: MASS class nnet spatial
Version: 7.2-42
Date: 2008-05-05
Depends: R (>= 2.4.0), grDevices, graphics, stats, utils
Suggests: lattice, nlme, survival
Author: S original by Venables & Ripley. R port by Brian Ripley
        , following earlier work by Kurt Hornik
        and Albrecht Gebhardt.
Maintainer: Brian Ripley 
BundleDescription: Functions and datasets to support Venables and
        Ripley, 'Modern Applied Statistics with S' (4th edition).
License: GPL-2 | GPL-3
URL: http://www.stats.ox.ac.uk/pub/MASS4/
Packaged: Mon May 5 06:06:09 2008; ripley
Package: MASS
Description: The main library and the datasets
Title: Main Package of Venables and Ripley's MASS
LazyLoad: yes
LazyData: yes
Built: R 2.7.1; i386-pc-mingw32; 2008-06-23 08:17:42; windows

-- File: C:/PROGRA~1/R/R-27~1.1/library/MASS/Meta/package.rds
I don't claim to understand all of that, but things you might want to know from this output are version number and dependencies (i.e., what the MASS package needs to find in order to work). Another way to get information is to ask for help about the package...
> library(help="MASS")
This will give you all of the above information plus a list of new functions and data sets that are contained in the package (in a new window if your OS is Windows). If you decide you need to use functions in the package, you can attach it to your search path like this...
> library("MASS")                      ### Note: require("MASS") also works
> search()
 [1] ".GlobalEnv"        "package:MASS"      "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"
Notice the package is attached at position 2 in the search path, meaning it will be searched immediately after the workspace. If you want it some place else, set the "pos=" option to the desired position. Once you're done with it and, perhaps, want it removed from your search path, you can do this...
> detach("package:MASS")               ### Note: NOT package="MASS"
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"
And it's gone!!

Sometimes attaching things to your search path causes conflicts, if there are identically named objects in two or more attached items. You'll be warned, but you may later want a reminder of what the conflicts were. Do this...

> conflicts()
[1] body<-
Well, that was somewhat unexpected!

To find out what's available in the way of optional (not installed by the R base installation) packages, go to The R Project for Statistical Computing home page, and click the "CRAN" link on the left side of the page under Download. (CRAN stands for Comprehensive R Archive Network.) Pick a mirror site you feel comfortable with (I'm partial to UCLA myself), and when the mirror site page loads, scroll down until you find something that says "Contributed extension packages" and click on it. I'm looking at the page right now, and under "Available Bundles and Packages" it says: "Currently, the CRAN package repository features 1473 objects including 1464 packages and 9 bundles containing 40 packages, for a total of 1504 available packages." So if I were you, I wouldn't plan on reading about all of them in one sitting!

Scroll down the page to the section of packages beginning with the letter U (or click the letter U at the top of the page). One of the packages listed there is called "UsingR". Click on it and some info about that package will be displayed. This package supports the Verzani (2005) textbook. Suppose you are intrigued and want to install this package. In R, type this command...

> install.packages("UsingR")
--- Please select a CRAN mirror for use in this session ---
trying URL 'http://www.ibiblio.org/pub/languages/R/CRAN/bin/windows/contrib/2.7/UsingR_0.1-8.zip'
Content type 'application/zip' length 1432870 bytes (1.4 Mb)
opened URL
downloaded 1.4 Mb

package 'UsingR' successfully unpacked and MD5 sums checked

The downloaded packages are in
        C:\Documents and Settings\kingw\Local Settings\Temp\RtmpGSgWbV\downloaded_packages
updating HTML package descriptions
You will be asked to select a CRAN mirror, and (in Windows) another window will open with a list of available mirror sites. Click on one close to you. (I just chose NC.) In a few seconds the download and installation will begin. If everything goes well, your R Console window will display messages similar to those above. If you now ask for help...
> library(help="UsingR")
... a new window will open with the help page for this package. There you can read about everything that has been added--mostly new data sets, but also some new functions, including one called simple.z.test( ) in the event you were really tied up in knots about not having a z-test function. To use the new package, add it to your search path, and away you go.

You can also update existing packages using the update.packages( ) function. And if you decide you really don't want some new package after all, use the remove.packages( ) function. (It's not a good idea to remove packages that are installed in the R base installation.) The help pages for these functions will tell you what you need to know to use them.

If you decide you want to write your own R package, there is an entire manual devoted to it that came with your download (in the Docs folder). Or you can find it online under the Documentation "Manuals" link. It's called "Writing R Extensions."


Return to the Table of Contents