# Introduction

GNU R offers a wide variety of packages for its users. There are all kinds of packages for R, which allow to display graphics or perform statistical tests. Some packages are designed for applications specific to a given industry. Many packages are already a part of the basic R installation, however, some of them need to be additionally installed into GNU R. This article will describe how to install and use packages under R.

# What is a Package

A **package** is a set of functions, help files and data files that have been linked together. In order to use a package in R you need to first make sure that it is installed in the local **library. **In general, the one system-level library is used for storing the default R packages. You can, however, add additional libraries. You also need to remember about loading packages into your current R session. This is very important when using R. It is recommended that you do not load too many packages at the time. Loading a large number of packages may result in errors due to clashes of function names coming from two different packages.

# Listing Packages in Local Libraries

In order to check for default packages installed with R it is possible to use the **getOption() **function as follows:

> getOption("defaultPackages")

[1] "datasets" "utils" "grDevices" "graphics" "stats" "methods"

In the list above the **base** package was omitted. This package is always loaded when you start R and it contains the elementary R functions.

You can also list the currently loaded packages by

> (.packages())

[1] "stats" "graphics" "grDevices" "utils" "datasets" "methods"

[7] "base"

In order to see all available packages we add the **all.available** option to the above R expression

> (.packages(all.available=TRUE))

[1] "base" "boot" "class" "cluster" "codetools"

[6] "compiler" "datasets" "foreign" "graphics" "grDevices"

[11] "grid" "KernSmooth" "lattice" "MASS" "Matrix"

[16] "methods" "mgcv" "nlme" "nnet" "parallel"

[21] "rpart" "spatial" "splines" "stats" "stats4"

[26] "survival" "tcltk" "tools" "utils"

Moreover, you can execute the** library()** function without arguments. This will output all available packages with a short description as shown below.

Packages in library ‘/usr/lib/R/library’:

base The R Base Package

boot Bootstrap Functions (originally by Angelo Canty

for S)

class Functions for Classification

cluster Cluster Analysis Extended Rousseeuw et al.

codetools Code Analysis Tools for R

compiler The R Compiler Package

datasets The R Datasets Package

foreign Read Data Stored by Minitab, S, SAS, SPSS,

Stata, Systat, dBase, ...

graphics The R Graphics Package

grDevices The R Graphics Devices and Support for Colours

and Fonts

grid The Grid Graphics Package

KernSmooth Functions for kernel smoothing for Wand & Jones

(1995)

lattice Lattice Graphics

MASS Support Functions and Datasets for Venables and

Ripley's MASS

Matrix Sparse and Dense Matrix Classes and Methods

methods Formal Methods and Classes

:

# Loading Packages

Let us now load a package **lattice.** This can be achieved with the **library()** function as indicated below.

> library(lattice)

In order to display the documentation related to the loaded package we execute the following command:

> library(help=lattice)

This will list the relevant information to the lattice package with the functions that can be used under such package.

Additional information can be obtained by typing in

>?Lattice

# Finding and Installing R Packages

One of the biggest sources of R packages is Comprehensive R Archive Network (CRAN). It is hosted by R Foundation, which also oversees the development of R. CRAN is hosted on several mirror sites around the world so pick the one closest to you to minimize the download times. For example, you can access a list of available R packages in CRAN. There is also relevant documentation available for every package listed in CRAN.

## Installation of R packages from Linux CLI

First, we describe how to install an R package from the Linux command line. Let's say we would like to install the "likelihood" R package. We can download it by:

$ wget -q http://cran.csiro.au/src/contrib/likelihood_1.5.tar.gz

Next we use the **R CMD INSTALL** command to install it. Please note that depending on the installation destination you may need to have a superuser privileges:

$ sudo R CMD INSTALL likelihood_1.5.tar.gz

[sudo] password for lubos:

* installing to library ‘/usr/local/lib/R/site-library’

* installing *source* package ‘likelihood’ ...

** package ‘likelihood’ successfully unpacked and MD5 sums checked

** R

** data

** demo

** preparing package for lazy loading

** help

*** installing help indices

** building package indices

** testing if installed package can be loaded

* DONE (likelihood)

**NOTE**: Be aware that some packages will require prerequisites. In that case use the above command to install prerequisites prior to your desired package.

## Installation of R packages from R console

There exists an R function for installing packages from the R console. This function will prompt you to select the mirror closest to your location and will install the desired package. Note the path where the package is being installed. You will need this path when you would like to remove the corresponding package.

> install.packages("likelihood")

Installing package(s) into ‘/home/renata/R/x86_64-pc-linux-gnu-library/2.15’

(as ‘lib’ is unspecified)

--- Please select a CRAN mirror for use in this session ---

Loading Tcl/Tk interface ... done

trying URL 'http://cran.csiro.au/src/contrib/likelihood_1.5.tar.gz'

Content type 'application/x-gzip' length 36789 bytes (35 Kb)

opened URL

==================================================

downloaded 35 Kb

* installing *source* package ‘likelihood’ ...

** package ‘likelihood’ successfully unpacked and MD5 sums checked

** R

** data

** demo

** preparing package for lazy loading

** help

*** installing help indices

** building package indices

** testing if installed package can be loaded

* DONE (likelihood)

The downloaded source packages are in

‘/tmp/RtmpWRwfqI/downloaded_packages’

Let us now remove the package **likelihood** from the corresponding location. This can be done as follows:

> remove.packages("likelihood", "/home/renata/R/x86_64-pc-linux-gnu-library/2.15")

Of course you need to specify your own path to your package.

# Conclusion

This article has taken as closer to using custom packages under GNU R. As you have seen the number of R packages is enormous resulting in the extremely wide applications of the GNU R software.

**GNU R tutorial series:**

**Part I: GNU R Introductory Tutorials:**

- Introduction to GNU R on Linux Operating System
- Running GNU R on Linux Operating System
- A quick GNU R tutorial to basic operations, functions and data structures
- A quick GNU R tutorial to statistical models and graphics
- How to install and use packages in GNU R
- Building basic packages in GNU R

**Part II: GNU R Language:**