1.5 Installing and loading packages

So far, all the functions we have used in this tutorial is part of your R installation. However, people all around the world have developed their own custom functions, and shared them in what we call packages6. During this course, we will need several of these packages, in this section you will learn how to install and load them.

Packages in R can be installed with the function install.packages(). You will only have to install a package once per computer. Here is how to install the ggplot2 package, which we will use next week:

install.packages("ggplot2")

When a package has been installed, you have to load it in order to use it. You have to load a package each time you want to use it in a script. You load a package using the function library():

library(ggplot2)

Make a habit of loading all the packages you use at the top of your script, that makes it easier to follow.

Important concept:
Packages contain custom functions that you will need later in this course (and during the rest of your studies and work as a biologist).

  • install a package with install.packages()

    • you only need to do this once per computer
  • load a package with library()

    • you need to do this every time you want to use that package. Load all packages you need at the top of your script

  1. There are thousands of packages available for use in R, doing anything imaginable. See for example the package ggbernie which is dedicated to plotting US senator Bernie Sanders, and allows me to make plots like this:

    ↩︎