Biostatistics with R

Handling the libraries and packages in R

In R, a package is a collection of a set of functions, associated data sets and related documents like manuals and tutorials. A given package is generally aimed at a particular type of analysis. For example, the stats package contains codes for statistical analysis, graphics package has scripts for graphics.


The R installation comes with a set of basic packages. In addition, thousands of packages for various analysis are kept at external repositories. These external packages can be downloaded from R prompt via internet. The downloaded package will be placed inside a directory inside R the installation. Once downloded, they can be included in R environment and used. We need internet connection for these downloads.


From R prompt, the command

> .libPaths()
will list the directories where the downloded packages are installed.

To get a list and small description of all the R packages installed in the system, call the list() function:

> list()


To install an external R package, use install.packages() function. For example, to install the package called cluster , type

> install.packages("cluster")


A package needs to be installed only once. The package can be included in a R script by calling library() function with package name as a string argument:

> library("cluster")

After the above call, all the functions inside library "cluster" will be availble inside the script.



The library function can also prints information on a given package :

> library(h="cluster")


The data sets inside a package can be listed by data() function:

> data(package="cluster")


A package attached by the library() can be removed by the detach() function:

> detach("package:cluster")


To display all the packages available in the repositories,

> new.packages()


In order to update all the installed packages with the newest version from repositories,

> update.packages()

The above two functions will prompt the user to choose a CRAN mirror site from the list of options.