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.
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:
This should print a bunch of info to the screen while it’s installing, then afterward we would load the library with:
But occasionally when using
install.packages() we will get a message like the following:
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 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:
Then, as we saw above, the dada2 installation instructions tells us to install it like so:
And again, after installing, we’d need to load the library in order to access the functions it contains, and we do that with
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:
And after that finishes up, again, in order to be able to use it, we just need to load it:
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.