The Art of R Programming

(WallPaper) #1
You can check which packages are currently loaded by typing this:

> .path.package()

B.2 Loading a Package from Your Hard Drive.....................................


If you need a package that is in your R installation but not loaded into mem-
ory yet, you can load it using thelibrary()function. For instance, suppose
you wish to generate multivariate normal random vectors. The function
mvrnorm()in the packageMASSdoes this. So, load the package as follows:

> library(MASS)

Themvrnorm()function will now be ready to use. And so will its docu-
mentation (before you loadedMASS, enteringhelp(mvrnorm)would have gener-
ated an error message).

B.3 Downloading a Package from the Web.......................................


The package you want may not be in your R installation. One of the big
advantages of open source software is that people love to share. People all
over the world have written their own special-purpose R packages, placing
them in the CRAN repository and elsewhere.

NOTE User contributions to CRAN go through a vetting process and are generally of high
quality. They are, however, not tested as throughly as R itself.

B.3.1 Installing Packages Automatically.................................


One way to install a package is to use theinstall_packages()function. For
example, suppose you wish to use themvtnormpackage, which computes
multivariate normal cumulative distribution functions and other quantities.
First, choose a directory in which you wish to install the package (and maybe
others in the future), say/a/b/c. Then at the R prompt, type this:

> install.packages("mvtnorm","/a/b/c/")

This will cause R to automatically go to CRAN, download the package,
compile it, and load it into a new directory:/a/b/c/mvtnorm.
You do need to tell R where to find that package once it’s installed,
which you can do via the.libPaths()function:

> .libPaths("/a/b/c/")

This will add that new directory to the ones R was already using. If you
use that directory often enough, you may wish to add that call to.libPaths()
in your.Rprofilestartup file in your home directory.

356 Appendix B

Free download pdf