7  Install & Update R Packages

Learning Objectives: After this chapter you will be able to:

  • Install a package from CRAN with install.packages().
  • Explain the difference between installing and loading a package.
  • Load a package with library() in each R session.
  • Update installed packages with update.packages().
  • Fix a “there is no package called” error.

7.1 Introduction

Packages add functions and data to R. Install a package once on your computer, then load it in each R session where you want to use it.

7.2 Install Packages

7.2.1 CRAN

We can install packages from CRAN using install.packages(). The name of the package must be enclosed in single or double quotes.

install.packages('ggplot2')

7.3 Load a package

install.packages() saves a package on your computer. library() makes it available in the current R session. You do not need to reinstall a package each time you open R, but you do need to load it again.

library(ggplot2)

If library(ggplot2) reports there is no package called 'ggplot2', install it with install.packages("ggplot2"), then run library(ggplot2) again.

7.4 Update Packages

Use update.packages() to check for and install package updates.

update.packages()

For other package sources, archived versions and library paths, see Package management.

Key points

  • Use install.packages() to install a package from CRAN on your computer.
  • Install once, then run library() in each session where you need the package.
  • A “there is no package called” error means you need to install that package before loading it.
  • Use update.packages() to check for and install package updates.

Next: RStudio Projects & Data Import