Installing R packages

Installing R packages

R


NOTICE
This page was initially put together before conda became a viable way to manage R environments. Now that that is possible, it’s generally easier to create new environments with the wanted version of R and needed packages that way, rather than installing a system-wide R and installing packages in there as done below. You can learn about installing and managing R and RStudio with conda on this page. I’d start there (or here first if new to conda), though the below may still be helpful if a specific package you need isn’t available through conda.




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

Keep in mind that the best way to install something is generally going to be the way the package documentation recommends. So that’s the first place we should start whenever possible. For instance, the dada2 package has it’s recommended method of installation right at the top of its documentation.





install.packages()

Often, we will be able to install packages with the install.packages() function. For example, if we want to install the glorious tidyverse package, we would run this:

install.packages("tidyverse")

This should print a bunch of info to the screen while it’s installing, then afterward we would load the library with:

library("tidyverse")

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

install.packages("dada2")
Warning in install.packages :
  package ‘dada2’ is not available for this version of R

But do not despair! This is usually just a consequence of the package not being maintained to be installable with this particular method. Doing a google search for install dada2 will allow us to find the installation instructions page, which tells us to use BiocManager::install() to install this particular package.



BiocManager::install()

BiocManager handles all of the packages hosted on Bioconductor. We do first need to install this before we can use it, using the above method like so:

install.packages("BiocManager")

Then, as we saw above, the dada2 installation instructions tells us to install it like so:

BiocManager::install("dada2")

And again, after installing, we’d need to load the library in order to access the functions it contains, and we do that with library("dada2").



install_github()

A semi-common way I also find myself using is to install the development version of something that is available on GitHub. We might want to do this, for example, if a bug was fixed in the code but it isn’t updated in the packaged release that is available with the above methods.

For one to do as an 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 from github:

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

And after that finishes up, again, in order to be able to use it, we just need to load it:

library("tidyr")




These are the 3 most common ways to install things in R. Finding the package documentation, and doing what they recommend is almost always the best way to go. If that isn’t possible for some reason, I’d try the above methods probably in the order they are presented above.