10.5 Packages

R functions are organized into collections of functions called packages. A number of packages are installed with R by default and several packages are loaded automatically in every R session. The search() function shows which packages are currently available, as shown below:



> search()



[1] ".GlobalEnv"        "package:stats"     "package:graphics"
[4] "package:grDevices" "package:utils"     "package:datasets"
[7] "package:methods"   "Autoloads"         "package:base"

The top line of the help page for a function shows which package the function comes from. For example, Sys.sleep() comes from the base package (see Figure 10.1).

Other packages may be loaded using the library() function. For example, the foreign package provides functions for reading in data sets that have been stored in the native format of a different statistical software system. In order to use the read.spss() function from this package, the foreign package must be loaded as follows:



> library(foreign)



The search() function confirms that the foreign package is now loaded and all of the functions from that package are now available.



> search()



 [1] ".GlobalEnv"        "package:foreign"   "package:stats"
 [4] "package:graphics"  "package:grDevices" "package:utils"
 [7] "package:datasets"  "package:methods"   "Autoloads"
[10] "package:base"

There are usually 25 packages distributed with R. Over a thousand other packages are available for download from the web via the Comprehensive R Archive Network (CRAN), http://cran.r-project.org. These packages must first be installed before they can be loaded. A new package can be installed using the install.packages() function (on Windows, there is an option on the Packages menu).

Paul Murrell

Creative Commons License
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 New Zealand License.