How to install (and launch) a software package (aka library)

Libraries are useful packages of functions and data sets contributed by R developers and other R users. R has a rigorous quality management system - its own archive of libraries called CRAN. All packages must pass strict tests to make sure that all functions are documented and work on all operation systems. Life scientists have their own package archive called BioConductor. At the same time, there are useful packages around outside of CRAN. Typically a researcher publishes their code on github along with their paper.

To use functions from a package, you must first install the package (unless the package has come with your RStudio installation) and then call (or "mount") it. The following code snippet makes it possible for the user to use for instance the read_tsv() function from the readr package. 

library(readr)

readr is a package that has many functions to read in files in different formats. To install it through your GUI, switch to the Packages tab in the bottom right RStudio pane and hit the Install button

Do not get scared by cryptical messages flashing on your screen. Everything tends to be read and many times you see error messages as well, but they tend to appear even if the package is installing correctly. 

By default, RStudio only offers you the CRAN repository to install packages from, as well as fromyour local computer (you will only use it when building and tuning your own package, which is way beyond the scope of this course). The programmatical way to do that would be calling

install.packages('readr')

To download packages from github, you can either configure the repositories selection in the GUI, or just do it programmatically.

First install the devtools package from CRAN. Then mount it by library(devtools). Finally call install_github("githubaccount_where_you_are_taking_your_package_from/package_name").

In some cases, you don't know on whose github account to look for the package. Then download the githubinstall package, also from CRAN and use its githubinstall function.

Back to Teaching Materials