In R, the fundamental unit of shareable code is the package. A package bundles together code, data, documentation, and tests and provides an easy method to share with others1. As of June 2016 there were over 8000 packages available on CRAN, 1000 on Bioconductor, and countless more available through GitHub. This huge variety of packages is one of the reasons that R is so successful: chances are that someone has already solved a problem that you’re working on, and you can benefit from their work by downloading their package.
To install packages:
# install packages from CRAN
install.packages("packagename")
As previously stated, packages are also available through Bioconductor and GitHub. To download Bioconductor packages:
# link to Bioconductor URL
source("http://bioconductor.org/biocLite.R")
# install core Bioconductor packages
biocLite()
# install specific Bioconductor package
biocLite("packagename")
And to download GitHub packages:
# the devtools package provides a simply function to download GitHub packages
install.packages("devtools")
# install package which exists at github.com/username/packagename
devtools::install_github("username/packagename")
Once the package is downloaded to your computer you can access the functions and resources provided by the package in two different ways:
# load the package to use in the current R session
library(packagename)
# use a particular function within a package without loading the package
packagename::functionname
For instance, if you want to have full access to the tidyr package you would use library(tidyr)
; however, if you just wanted to use the gather()
function without loading the tidyr package you can use tidyr::gather(function arguments)
.
For help on packages that are installed on your computer:
# provides details regarding contents of a package
help(package = "packagename")
# see all packages installed
library()
# see packages currently loaded
search()
# list vignettes available for a specific package
vignette(package = "packagename")
# view specific vignette
vignette("vignettename")
# view all vignettes on your computer
vignette()
Note that some packages will have multiple vignettes. For instance vignette(package = "grid")
will list the 13 vignettes available for the grid package. To access one of the specific vignettes you simply use vignette("vignettename")
.
There are thousands of helpful R packages for you to use, but navigating them all can be a challenge. To help you out, RStudio compiled a guide to some of the best packages for loading, manipulating, visualizing, analyzing, and reporting data. In addition, their list captures packages that specialize in spatial data, time series and financial data, increasing spead and performance, and developing your own R packages.