R Programming/Packages

From Wikibooks, open books for an open world
Jump to: navigation, search

An R package includes a set of functions and datasets. Packages are often developed as supplementary material to books. For instance the MASS package was developed by Venables and Ripley for their book Modern Applied Statistics with S and the car package was developed by John Fox for his book An R and S plus Companion to Applied Regression.

Load a package[edit]

A package is loaded into the current R environment using the library() function. A list of functions and datasets included in a package can be obtained by using the h or help argument of the library function.

library("stats4") # loads the package "stats4"
library(h=stats4) # gives help for all functions
data(package="stats4") # gives the list of all available datasets

A package can be detached from the current environment by using the detach() function :

> detach("package:prettyR")

Without any arguments the library() function lists all of the packages currently available to the user. env() (gdata) describe all loaded environments (ie packages). search() gives the list of all loaded packages.

> library() # returns the description of all the packages available on the computer 
> dir(.libPaths()) # returns the name of all the packages available on the computer (quicker than the previous one)
> search()
> env(unit="MB")

current.packages() (Zelig) show all the required and suggested packages.

> current.packages("sem")

Where are my packages stored ?

  • The .libPaths() function without arguments prints the library directories
  • The .libPaths() function with a directory as argument defines a new directory where to store new libraries.
> .libPaths()
[1] "/Users/username/Library/R/library"    
[2] "/Library/Frameworks/R.framework/Resources/library"
> .libPaths("W:/AppData/R/library")

Install new packages[edit]

  • The exhaustive list of all available packages is on the CRAN website
  • To install a new package, you generally just have to specify the name of the package as an argument of install.packages() function.
  • Sometimes you need to specify more options. For instance, this is the case if you are not an administrator of your computer.
    • "lib" specifies the directory where you want to store the package
    • "repos" specifies a list of repositories. Note that you can specify of vector of repositories.
    • "dep=T" specifies that all the required packages are also downloaded and installed.
> install.packages("faraway")
> install.packages("rgrs", lib="W:/AppData/R/library" , 
repos=c("http://r-forge.r-project.org","http://cran.fr.r-project.org/"), 
dep=TRUE)
  • Stay up to date.

If you want to be aware of the latest packages, type new.packages() in R or visit the Revolution Computing Blog which gives each month a list of the new and the updated packages.

> new.packages() # displays all the packages available in the repositories
> update.packages() # updates all the packages installed with the newest version available in the repositories

We can also install bundles of packages using install.views() or update.views() (ctv).

> install.packages("ctv") 
> library("ctv") 
> install.views("Econometrics") 
> update.views("Econometrics")

We can also remove packages with remove.packages().

Building R Packages[edit]

You can write down your own R packages. But, all packages submitted to CRAN (or Bioconductor) must follow specific guidelines, including the folder structure of the package and the other files like DESCRIPTION, NAMESPACE and so on.


  • See Friedrich Leisch's introduction (PDF 20 pages)[1]
  • See also Duncan Murdoch's tools for building packages using Windows[2]

References[edit]

Previous: Settings Index Next: Documentation