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, we simply need to install it and then load it. Here we’ll cover the 3 main ways for installing packages.




install.packages()

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

install.packages("ggplot2")

And we get some info printed to our screen such as this:


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

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


But do not despair! This is usually just a consequence of the package not being maintained to be installable this particular way, and we can almost always get around it by installing from Bioconductor{target=”_blank”} directly. When we do run into this, we should head right on over to google and search with terms for the package name and bioconductor, and we’ll most likely find a way to install with BiocManager::install().



BiocManager::install()

BiocManager handles all of the packages hosted on Bioconductor{target=”_blank”}. And is my usual next attempt if install.packages() does not work. If we don’t have it yet, this can be installed with:

install.packages("BiocManager")

Then, for example, searching for “phyloseq bioconductor” returns the bioconductor homepage of the phyloseq package as the top hit. And when we head over there, there are directions to install directly from Bioconductor:

BiocManager::install("phyloseq")

And again, after installing, we’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 we 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, we just need to load the library for use
library("tidyr")