Loading Packages
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.
Loading Packages on Windows and Linux
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.
Loading Packages on Mac OS X
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access