B.1 R Package Installation Basics

You can install R packages from the R command line or from your system’s command line.

R package installation basics are outlined in Chapter 6 of the R Installation and Administration Guide. The following example installs a package on Oracle Linux using Oracle R Distribution. It installs the arules package as root so that packages are installed in the default R system-wide location where all users can access it, /usr/lib64/R/library.

Within R, using the install.packages function always attempts to install the latest version of the requested package available on CRAN:

R> install.packages("arules")

If the arules package depends upon other packages that are not already installed locally, the R installer automatically downloads and installs those required packages. This is a huge benefit that frees users from the task of identifying and resolving those dependencies.

You can also install R from the shell command line. This is useful for some packages when an internet connection is not available or for installing packages not uploaded to CRAN. To install packages this way, first locate the package on CRAN and then download the package source to your local machine. For example:

$ wget http://cran.r-project.org/src/contrib/arules_1.1-9.tar.gz

Then, install the package using the command R CMD INSTALL:

$ R CMD INSTALL arules_1.1-9.tar.gz

A major difference between installing R packages using the R package installer at the R command line and shell command line is that package dependencies must be resolved manually at the shell command line. Package dependencies are listed in the Depends section of the package's CRAN site. If dependencies are not identified and installed prior to the package's installation, you will see an error similar to:

ERROR: dependency 'xxx' is not available for package 'yyy'

As a best practice and to save time, always refer to the package's CRAN site to understand the package dependencies prior to attempting an installation.

If you don't run R as root, you won't have permission to write packages into the default system-wide location and you will be prompted to create a personal library accessible by your userid. You can accept the personal library path chosen by R, or specify the library location by passing parameters to the install.packages function. For example, to create an R package repository in your home directory:

R> install.packages("arules", lib="/home/username/Rpackages")

or

$ R CMD INSTALL arules_1.1-9.tar.gz --library=/home/username/Rpackages

Refer to the install.packages help file in R or execute R CMD INSTALL --help at the shell command line for a full list of command line options.

To set the library location and avoid having to specify this at every package install, simply create the R startup environment file .Renviron in your home area if it does not already exist, and add the following piece of code to it:

R_LIBS_USER = "/home/username/Rpackages"