2.3 Working with packages
In R, the fundamental unit of shareable code is the package. A package bundles together code, data, documentation, and tests to easily share analysis methods with others8. There are currently 14956 packages available on the Comprehensive R Archive Network (CRAN) and countless more available from Bioconductor and GitHub. The huge variety of packages is a key reason why 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.
2.3.1 Installing Packages
To install a package from the CRAN, use the function
where “packagename” is replaced by the name of a package that has been published on the CRAN. A list of packages on the CRAN, sorted by name, can be viewed here
As previously stated, packages are also available through Bioconductor and GitHub. Prior to installing a Bioconductor we must first requires download and install Bioconductor itself:
# 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:
2.3.2 Loading Packages
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)
.
2.3.3 Getting help with packages (from within R)
The help documentation and support in R is comprehensive and easily accessible from the command line. To leverage general help resources you can use:
help.start() # provides general help links
help.search("text") # searches the help system for documentation matching a given character string
Note that the help.search("some text here")
function requires a character string enclosed in quotation marks.
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")
.
2.3.4 Getting help with packages (from the web)
Typically, a problem you may be encountering is not new and others have faced, solved, and documented the same issue online. The following resources can be used to search for online help. Although, I typically just google the problem and find answers relatively quickly.
RSiteSearch("key phrase")
: searches for the key phrase in help manuals and archived mailing lists on the R Project website.- Stack Overflow: a searchable Q&A site oriented toward programming issues. 75% of my answers typically come from Stack Overflow.
- Cross Validated: a searchable Q&A site oriented toward statistical analysis.
- R-seek: a Google custom search that is focused on R-specific websites
- rdocumentation.org: Interactive documentation for CRAN, BioConductor and Github packages
- R-bloggers: a central hub of content collected from over 500 bloggers who provide news and tutorials about R.
2.3.5 Finding useful packages
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.