Installing packages

There are several repositories from which you can install R packages. Here are a few:

  • The CRAN (for Comprehensive R Archive Network),
  • Bioconductor,
  • GitHub.

There are others, but these three are really the most important.

CRAN

It is the "original" repository which provides (at the time of this tutorial) 17618 packages. The list of these packages is available here.

The command to install a package from this source is

install.packages("package_name")

This command will install the package_name package in the default library of your session. To find out exactly where this library is located, you can run the command

.libPaths()

The result depends on a lot of things, including which operating system and which version of R you are using. At home, the result is the following:

"/Library/Frameworks/R.framework/Versions/4.0/Resources/library"

Bioconductor

This repository is essential for users who wish to do bioinformatics with R. It provides 1974 software packages, 398 experiment data packages, 968 annotation packages, and 28 workflows on the date when this tutorial has been written. You can find essential packages on Bioconductor like [limma] (https://bioconductor.org/packages/release/bioc/html/limma.html) for doing differential analysis of -omics data, or again [org.Hs.eg.db] (https://bioconductor.org/packages/release/data/annotation/html/org.Hs.eg.db.html) which contains annotations of the human genome. All of this, and more, is available [at this address] (https://bioconductor.org).

To install a Bioconductor package, you will need to use the function

BiocManager::install("package_name")

Please note, to use this function, you must first install the BiocManager package! (with the command install.packages("BiocManager"))

GitHub

Many R programmers (including me) use a version control system to work, and some use GitHub to host their efforts. Recently, it has been possible to install packages directly from these hosting systems. Be careful, sometimes it's the development version of a package that is on GitHub and not the most stable version!

To install a package from GitHub, you can use the command

remotes::install_github("username/package_name")

To use this command, you must first install the remotes package! (with the command install.packages("remotes"))

Loading packages

There are two functions in R that allow you to load packages: require andlibrary, which are used in the same way.

require(package_name)
require("package_name")
library(package_name)
library("package_name")

If the package exists

When the package package_name exists, both functions allow the use of functions from that package. The difference between the two is in what the function "returns"

  • library returns the packages already loaded,
  • require returns TRUE

Exercise: assign to an object called obj1 andobj2 the result, respectively, of the command library(limma) and of the command require(limma).

obj1 <- library(limma)
obj1
obj2 <- require(limma)
obj2

If the package does not exist

The most important difference between these two functions appears when we want to load a package which does not exist, in this case,

  • library generates an error,
  • require generates a warning, and returns the value FALSE.

Exercise: assign to an object called obj1, resp. obj2, the result of the library(limma) command and the require(limma) command, respectively.

obj1 <- library(licorne)
obj1
obj2 <- require(licorne)
obj2

Which function to use?

There are other differences between the two functions, cf. the common help page of the two functions accessible with the command ?library or ?require.

My advice: use library as it will generate an error if you forgot to install a package!