By default, not all packages are loaded into R. If you try to use a function from a package that hasn’t been loaded, you’ll get an error:
> # try to use rpart
before loading it
> fit <- rpart(Kyphosis ~ Age + Number + Start, data=kyphosis)
Error: could not find function "rpart"
To load a package in R, you can use the library()
command. For example, to load the package rpart
(which contains functions for building
recursive partition trees), you would use the following command:
> library(rpart)
(There is a similar command, require()
, that takes slightly different
arguments. For more about require
, see
the R help files.)
If you’re more comfortable using a GUI, you can browse for packages
and load them using the GUI. If you choose to use this interface to find
packages, make sure that you include the appropriate library
command with your scripts to prevent
errors later.
On Microsoft Windows, you can use the library
function to load packages.
Alternatively, you can select “Load package” from the Packages menu in
the GUI. This will bring up a window showing a list of packages that you
can choose to load.
The Mac OS X R environment is a little fancier than the
other versions. Like the other versions, you can use the library()
function. Otherwise, you can select
Package Manager from the “Packages
& Data” menu. The Package Manager UI, as shown in Figure 4-1, lets you see which packages are
loaded, load packages, and even browse the help file for a
package.
Get R in a Nutshell, 2nd Edition 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.