Installing & Loading Packages in R

Interactive beginner R tutorial on packages: install.packages(), library(), CRAN, and calling a function with package::function. Run real R in your browser.

Base R is powerful, but R’s real strength is the 20,000+ packages on CRAN. Everything below is a live R console β€” edit the code and press Run Code.

Watch: installing & loading packages

install.packages() once, library() each session, and package::function() to reach in for a single tool.

Download the R script  Β·  β–Ά Practice in the playground β€” or edit and run each snippet below.

Install once, load each session

On your own machine you install a package once from CRAN, then load it whenever you need it:

install.packages("dplyr")   # once, downloads from CRAN
library(dplyr)              # every session, to use it

Load a package and use it

MASS ships with R. Loading it makes its datasets and functions available:

Note

In this browser, packages are installed with webr::install() instead of install.packages(). The first Run Code below fetches MASS β€” give it 15–30 seconds.

Now its tools are yours

Reach for one function without attaching

package::function() uses a single function without library() β€” handy to avoid name clashes:

Your turn


Next: Your first plot β†’ β€” turning data into pictures.