Installing R packages

Installing R packages

R

Part of what makes R so valuable is that there is an enormous community of people developing software packages for it. People share bundles of code that perform specific tasks through what are known as “packages”. Packages are typically maintained at the Comprehensive R Archive Network (CRAN) and/or at Bioconductor. To use a package, you simply need to install it and then load it. Here we’ll cover the 3 main ways for installing packages.




install.packages()

Most often, you will be able to install packages with the install.packages() function. For example, if you want to install the ggplot2 package, you would simply enter:

install.packages("ggplot2")

And you’ll get some info printed to your screen such as this:


And all is well with the world and you are ready to load the package with library("ggplot2"), at which point you’re ready to rock.

But occasionally when using install.packages() you will get a message like the following:


But do not despair! This is usually just a consequence of the package not having been updated to install this particular way, and you can almost always get around it by installing from bioconductor. When you do run into this, you should head right on over to google and search with terms for the package name and bioconductor, and you’ll most likely find a way to install with biocLite().



biocLite()

For example, searching for “phyloseq bioconductor” returns the bioconductor homepage of the phyloseq package as the top hit. And when you head over there, there are directions to install via bioconductor:

source("https://bioconductor.org/biocLite.R")
biocLite("phyloseq")

And sure enough that seems to work just swell:


And again, after installing, you’d need to load the library in order to access the functions it contains: library("phyloseq").



install_github()

Occasionally bioconductor may also not workout. At that point I usually turn to searching for the package on github as it may be hosted there, and if so it usually contains instructions for how to install it via devtools and the install_github() function.

For example, a little googlation for “tidyr github” returns as the top hit the tidyr package github page, and if you scroll down a little bit there are installation instructions that include how to install.

install.packages("devtools")
devtools::install_github("tidyverse/tidyr") 

  # and after that finishes up, you'd just need to load the library for use
library("tidyr")