R Programming/Packages< R Programming
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 packageEdit
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
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.
Where are my packages stored?
.libPaths()function without arguments prints the library directories
.libPaths()function with a directory as argument defines a new directory where to store new libraries.
> .libPaths()  "/Users/username/Library/R/library"  "/Library/Frameworks/R.framework/Resources/library" > .libPaths("W:/AppData/R/library")
Install new packagesEdit
- Each major distribution of R includes a 'base' set of packages which support many basic statistical functions.
- Many R Users also choose to install additional 'Add-on' packages to provide simplified interfaces to R commands or to add specialist functionality i.e. the ggplot Grammar of Graphics package provides an advanced graphical output capability.
- The exhaustive list of all available packages is on the CRAN website.
- The R community has developed a vast resource of Add-on packages, some with unique functionality, some with overlapping functionality. It is therefore common to find multiple R packages capable of completing the same task i.e. reading and writing Excel spreadsheets. Ultimately which package to use is your choice.
- To install a new package, it is usually necessary 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 a 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.packages("ctv") > library("ctv") > install.views("Econometrics") > update.views("Econometrics")
We can also remove packages with
Package Documentation and HelpEdit
All R packages install with 'help' documentation, listing their functions and providing syntax and usage examples.
> library("tidyr") # load the tidyr package > help("tidyr") # view the tidyr package's help documentation
See the Obtaining Help Documentation section for more details on accessing package 'help' documentation.
- Most R packages have dependencies or references to other R packages. You must have all of an R package's 'required' dependencies installed, before you can use the package.
- R package dependencies come in two types, required and suggested.
- Specialist R packages such as the ggplot Grammer of Graphics package have large package dependency trees.
install.packages()function will automatically download and install a package and its dependencies, on a computer with an Internet connection.
- The R CMD INSTALL utility will check preinstalled packages for dependencies, but not download missing packages.
- Users must follow separate package download and installation processes when working on a computer with no Internet connection. The miniCRAN package can be used to assist in the offline management of R package dependencies.
Building R PackagesEdit
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.
- Friedrich Leisch Creating R Packages : A Tutorial http://cran.r-project.org/doc/contrib/Leisch-CreatingPackages.pdf