Installing & Loading
Packages in R
Learn to extend R's power using install.packages() and library() to access thousands of community-built tools.
What Is an R Package?
A package is a collection of R functions, data sets, and documentation bundled together. Think of it like an app for your phone: R comes with basic features, but packages let you add specialized capabilities.
Why Packages Matter
Base R is powerful, but packages unlock the full ecosystem. Over 20,000 packages are available on CRAN (the Comprehensive R Archive Network), covering everything from data visualization to machine learning.
The Two-Step Process
Using a package in R always involves two distinct steps. Understanding this separation is the key to avoiding confusion.
install.packages() Explained
The install.packages() function downloads a package from the internet (CRAN) and saves it to your computer's R library folder. You only need to do this once per package.
library() Explained
The library() function loads an already-installed package into your current R session. You must call this at the start of every new session to use a package's functions.
Install Once, Load Every Time
| install.packages() | library() | |
|---|---|---|
| Purpose | Downloads from internet | Activates in session |
| Frequency | Once per package | Every new R session |
| Quotes Required? | Yes (always) | Optional |
| Internet Needed? | Yes | No |
| Analogy | Download an app | Open the app |
Where Packages Live
Step-by-Step Workflow
Example 1: Installing ggplot2
You want to create a scatter plot. The ggplot2 package is the go-to tool for data visualization in R. Here's the complete workflow:
Example 2: Multiple Packages
Real projects often need several packages. You can install and load them one by one, or install multiple at once using a vector.
Example 3: Checking Installed Packages
Before installing, you might want to check if a package is already installed. R provides helpful utility functions for this.
library() vs require()
You may encounter require() as an alternative to library(). While both load packages, they behave differently when something goes wrong.
| library() | require() | |
|---|---|---|
| If package missing | Throws an error | Returns FALSE (warning) |
| Stops your code? | Yes | No |
| Best for | Scripts and analysis | Inside functions |
| Recommended? | ✓ Yes, use this | Only in special cases |
The R Package Ecosystem
Packages don't all come from the same place. Here are the three main sources you should know about:
Common Mistakes
Question 1 of 5
Which function downloads a package from CRAN to your computer?
Question 2 of 5
Which of the following is the correct way to install the dplyr package?
Question 3 of 5
You installed ggplot2 yesterday. Today you open a new R session. What must you do before using ggplot()?
Question 4 of 5
How do you install multiple packages in a single command?
Question 5 of 5
What is the main difference between library() and require() when a package is not installed?