— Philippe Grosjean 2006/01/18 [last tested: 2006/02/01 on R 2.2.1]
The official documentation for such a task is included in the R Installation and Administration, a PDF manual installed with each R version. Here is a quick mini ‘HowTo’ summarizing the operations for the easiest way, that is, by using
Look at CRAN to determine if a binary version exists for your system.
Otherwise, you still can compile packages from sources. The simplest way to download a package from CRAN and to install it is by using the
install.packages() function (make sure you install also all required dependencies). For example, to download and install the package called “mypkg”:
install.packages("mypkg", dependencies = TRUE)
Of course, this only works if your computer is connected to the Internet and if all tools required to compile packages are installed (see the manual if it fails).
Another way of installing a package is in
batch mode that is, without starting R you should download the package tarball (i.e. package-name.tar.gz) file you want to install and then issue as root from your usual shell (bash, csh, sh,etc.) and under the same directory in which the tarball is:
R CMD INSTALL package-name.tar.gz
Using Rgui, the default R front-end under Windows, you can install packages from the menu:
Packages -> Install package(s).... The first time, R asks for a mirror. Choose the one nearest to you in the list. Then, select the package(s) you want to install in the list. If the operation fails, look at the manual and the
RW-FAQ for more information.
You can use
R CMD INSTALL the same way as for Linux/Unix. The Mac GUI also provides a Package Installer for convenient point-and-click installation and updating of packages from various repositories.
Unix users should be aware that the default package type for
install.packages() on Mac OS X is binary unlike on other unix systems.
Do not forget to update your packages regularly from the latest CRAN version! This can be done very easily using
update.packages() or the equivalent menu entries in the various R GUIs.
A R package is a plugin providing additional functions and/or example datasets together with online help, and sometimes “vignettes“1). A R library is a location (top directory) where one or several packages are installed.
If you do not already know the name of the package you want to install, browse the list of packages on CRAN (http://cran.r-project.org/web/packages/, where you can access the PDF manual of each package to learn more about the functions they provide) and on Bioconductor (http://www.bioconductor.org/docs/vignettes.html, vignettes for all bioconductor packages). Alternatively, you can look at the CRAN Task Views (http://cran.r-project.org/src/contrib/Views/) that present lists of useful packages for given applications.
Always remember: if you want to use functions and/or datasets in an optional package, you must first load the package in your current R session. This is done with the
library(mypkg) # From this moment on, you have acces to functions in 'mypkg' data(mydataset) # Always remember also to load datasets with data() before using them # ... Here comes your computations using 'mypkg' functions and/or 'mydataset' # If you don't need 'mypkg' any more, you can unload it with: detach("package:mypkg")
To have libraries loaded by default when R is launched for all users on a computer, add code to the “R_HOME/etc/Rprofile.site” file. For instance, the code below automatically loads the ggplot2 library (and it’s dependency libraries) when R is launched.