Introduction to R Packages

In R language functions and datasets are all stored in packages. The content of a package is only available when a package is loaded using the library() function.

To see which R packages are installed, write the following command (without argument)

> library()

To load a particular installed package, use package name as the argument to the library() function, that is,

> library(MASS)

If the computer system is connected to the internet and a required package is not installed on one’s computer, the user can use the install.packages() function to install the required package. To update the already installed package one can use the update.package() function. The search() function can be used to see which packages are loaded into computer memory.

R packages can be classified as standard (base) packages and contributed packages. The standard (or base) packages are considered part of the R source code. The base packages contain the basic functions that allow R to work. The base packages also contain datasets, standard statistical and graphical functions. The standard R functions are automatically available in any R installation, that is, you don’t need to install them.

The standard R packages are written by authors. These packages implement some specialized statistical methods, access to datasets and hardware. The contributed packages are distributed with every binary distribution of R and are available for download from CRAN and other repositories such as BioConductor.

R packages can have a namespace. Namespaces (i) allows the package writer to hide functions and data that are meant only for internal use, (ii) prevent functions from breaking when a user picks a name that clashes with one in the packages, and (iii) they provide a way to refer to an object within a particular package.

For example, in R the t() function is the transpose function. A user can define his own t() function. The namespaces will prevent the user’s definition from taking procedure and breaking every function that tries to transpose matrix.

There are two operators that work with namespaces, (i) :: double colon operator and triple colon operator :::. The double colon operator selects definitions from a particular namespace. For example, t() function is available as the base::t, because it is defined in the base package. The function that is exported from the package can be retrieved with a double colon operator.

The tiple colon operator acts as a double colon operator but it also allows access to hidden objects. The getAnywhere() function can be used to search for multiple packages.

Note: Packages are inter-dependent, and loading one package may cause other packages to be automatically loaded. The colon operators also cause automatic loading of the associated package. the package is not added to the search list when a package with namespaces is loaded automatically.