Technical Note - Installing Your Own R Packages
The R language is widely used across the ECS and SMS schools.
Many users who wish to make use of add-on R packages, have to wait for the
package to be installed in the "R system" area before they can make use of it.
R allows users to install personal copies of add-on packages, that co-exist with
its "system" packages.
This Tech Note describe a "School standard" way in which users can install
and try out packages for themselves, before asking that a package be installed
This is a technical note. If you do not want the text within it wrapped and/or have
no interest in navigating away via the space wasting but University-standard look
and feel side bars, you can get rid of them by adding
to the URL
Nomenclature: An R library
is a collection of R packages
. Packages must first be installed
into a library, then they may be loaded
from that library into memory once R is running. The
command inside R is used either to list the packages contained within the available libraries, or to load an individual package into memory from a library.
The "R Installation and Administration" manual, normally found at
has a number of sections dedicated to this topic, once section of which
notes an internal R variable
, which, on the Schools'
ArchLinux systems returns the following mouthfull
however, given the homogeneity of the Schools' ArchLinux systems, the long folder name is not required and so it is
suggested that the more simple location
be used for personal packages. A further advantage of using the short name is that once a package is installed, it will still be available for subsequent upgrades of R. However note that sometimes an installed package may be incompatible with a newer version of R, but R will warn if it is necessary to upgrade the package. There is another advantage noted later in this document.
One advantage of using the default package location is that R will automatically use it if it exists, and will offer to create it when you try to install your first package.
How R finds installed packages
On the Schools' Linux systems, an environmental variable
to the following value
which has the effect of adding that directory to R's standard library search path
because this is the path below which extra packages available across the whole system
get installed. We'll call these school-wide packages.
In order to make use of packages installed elsewhere, you merely need to
the extra path to that environmental variable, before invoking R, eg
$ export R_LIBS
% setenv R_LIBS ~/Rlibs:/vol/R/x86_64linux/library/
or in your personal
% echo "R_LIBS=~/Rlibs:/vol/R/x86_64linux/library/" > ~/.Renviron
Note that in general you want your own folder location before the system ones so that you have the option to 'override' the already installed version of a package.
You can also do this inside of R itself, using the
When you initially fire up R on a School workstation,
the bare function shows you the current set of library search paths known to R:
and it might be instructive to take a look at that path to see what's provided by the system
$ ls /usr/pkg/lib/R/library
KernSmooth/ cluster/ graphics/ nnet/ splines/ translations/
MASS/ codetools/ grid/ parallel/ stats/ utils/
Matrix/ compiler/ lattice/ rkward/ stats4/
base/ datasets/ methods/ rkwardtests/ survival/
boot/ foreign/ mgcv/ rpart/ tcltk/
class/ grDevices/ nlme/ spatial/ tools/
Be aware that R doesn't recognise an automounted 'alias' and so the
is really the path we know as
If you alter your environment using the personal customisation, you'll see (in Bourne shell e.g.)
although, once again, the nature of automounted file systems make it less clear than it
might be that
has been added as a library search path.
Finally, here's an example of how the
function, when supplied with a parameter,
can manipulate the library search path, from inside R.
then specify our personal path
 "/am/roxy/home/fred/Rlibs" "/usr/pkg/lib/R/library"
but note that this has removed the path that the
added, so to avoid this, use
> .libPaths(c("~/Rlibs", .libPaths()))
If one wishes to use a 'personal' or updated version of a package in preference to the school-wide installed package but still have most school-wide packages available, one cannot use the default location of
because the school-wide path provided by
takes precedence over the
path. Thus this is another advantage of using a non-default personal folder name and specifying it either via the
environment variable, or via
How to install R packages
There are two basic ways to install packages;
- install from a package file from outside of R
- use R to download and install the package
The first method is required if one has modified an existing package or has downloaded a package from somebody's personal web site. The second method is preferable, since it will always install the most recent update of a package for whichever version of R is running, and will also install any package dependencies (i.e. other packages that the primary package requires).
Installing from a downloaded file
On a Linux system, R packages are always installed from source. A source package for Linux will be of the form
. Note that while at the Linux level a file named
is expected to be identical, this latter is typically used for the name of a Macintosh binary
package file for R.
The procedure for installing such a package (in a C shell) is:
% setenv R_LIBS ~/Rlibs:$R_LIBS
% R CMD INSTALL pkg_1.23-45.tar.gz
This will check that required packages ("dependencies") are already installed and then compile (if necessary) and install the named package into the users private area.
Note that if this is the first package you are installing in the named library, the folder (
in this case), must pre-exist.
An alternative one-line method is as follows:
% R CMD INSTALL --library="~/Rlibs" pkg_1.23-45.tar.gz
Note that with this method, all dependencies would have to be available either in the system library or the user's private library; in particular the school-wide library is not available.
Installing from within R
R can be set up very easily to install or update packages that can be found on a CRAN website. It is theoretically possible to use locations other than CRAN or its mirrors, but the information required to set that up is beyond the brief of this technical note.
> install.packages("<primary package name>",
Once again, The "R Installation and Administration" manual, normally found at
may be of use, if you wish to go beyond the brief of this technical note.