How to Find and Add Packages to Your R Installation
If you’re just getting started with R, you’ve probably used only functions that are available in the basic installation of R. But the real power of R lies in the fact that anyone can write their own functions and share them with other R users in an organized manner.
Many knowledgeable people have written convenient functions with R, and often a new statistical method is published together with R code. Most of these authors distribute their code as R packages (collections of R code, Help files, datasets, and so on that can be incorporated easily into R itself).
Several websites, called repositories, offer a collection of R packages. The most important repository is the Comprehensive R Archive Network (CRAN), which you can access easily from within R.
In addition to housing the installation files for R itself and a set of manuals for R, CRAN contains a collection of package files and the reference manuals for all packages. For some packages, a vignette (which gives you a short introduction to the use of the functions in the package) is also available. Finally, CRAN lets you check whether a package is still maintained and see an overview of the changes made in the package. CRAN is definitely worth checking out!
You install a package in R with the function — wait for it — install.packages(). Who could’ve guessed? So, to install the fortunes package, for example, you simply pass the name of the package as a string to the install.packages() function.
The fortunes package contains a whole set of humorous and thought-provoking quotes from mailing lists and help sites. You install the package like this:
R may ask you to specify a CRAN mirror. Because everyone in the whole world has to access the same servers, CRAN is mirrored on more than 80 registered servers, often located at universities. Pick one that’s close to your location, and R will connect to that server to download the package files. In RStudio, you can set the mirror by choosing Tools→Global Options→Packages.
Next, R gives you some information on the installation of the package:
Installing package(s) into ‘D:/R/library’(as ‘lib’ is unspecified) .... opened URL downloaded 165 Kb package ‘fortunes’ successfully unpacked and MD5 sums checked ....
It tells you which directory (called a library) the package files are installed in, and it tells you whether the package was installed successfully. Granted, it does so in a rather technical way, but the word successfully tells you everything is okay.
Loading and unloading packages
After a while, you can end up with a collection of many packages. If R loaded all of them at the beginning of each session, that would take a lot of memory and time. So, before you can use a package, you have to load it into R by using the library() function.
You load the fortunes package like this:
You don’t have to put quotation marks around the package name when using library(), but it is wise to do so.
Now you can use the functions from this package at the command line, like this:
> fortune(“This is R”)
The library is the directory where the packages are installed. Never, ever call a package a library. That’s a mortal sin in the R community. Take a look at the following, and never forget it again:
You can use the fortune() function without arguments to get a random selection of the fortunes available in the package. It’s a nice read.
If you want to unload a package, you’ll have to use some R magic. The detach() function will let you do this, but you have to specify that it’s a package you’re detaching and that you want to unload it, like this:
> detach(package:fortunes, unload=TRUE)
Actually, even this line of code doesn’t always unload a package. For example, if a package is used by another package that’s still loaded, that code won’t work. If you’ve been toying around in R for a while and tried to load and unload many packages, save your work, close R, and start a fresh session.