01 / 20
R +

Installing & Loading
Packages in R

Learn to extend R's power using install.packages() and library() to access thousands of community-built tools.

R Programming

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.

Functions
Reusable code that performs specific tasks, like making charts or cleaning data.
Data
Built-in datasets for practice, testing, and demonstration purposes.
Documentation
Help pages and vignettes explaining how to use each function correctly.

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.

ggplot2
Creates beautiful, publication-quality visualizations.
dplyr
Makes data manipulation fast and intuitive.

The Two-Step Process

Using a package in R always involves two distinct steps. Understanding this separation is the key to avoiding confusion.

Step 1: INSTALL install.packages() Downloads from CRAN Step 2: LOAD library() Activates for use Analogy Install = Download an app ★ Load = Open the app

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.

R Console
# Install the ggplot2 package install.packages("ggplot2")

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.

R Console
# Load the ggplot2 package library(ggplot2)

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

CRAN 20,000+ packages online install.packages() Your Computer's R Library Packages stored on hard drive library() Your Active R Session Functions now available to use

Step-by-Step Workflow

1
Identify the package you need. Search CRAN or ask: "What package does X?" For example, ggplot2 for plotting.
2
Install it once. Run install.packages("package_name") in the R console. Wait for the download to complete.
3
Load it in your script. Add library(package_name) at the top of your R script or notebook.
4
Use the package's functions. You can now call any function from that package in your code.

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:

R Script
# Step 1: Install (only run this once) install.packages("ggplot2") # Step 2: Load (run every session) library(ggplot2) # Step 3: Use it! ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()

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.

R Script
# Install multiple packages at once install.packages(c("dplyr", "tidyr", "readr")) # Load each one separately library(dplyr) library(tidyr) library(readr)

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.

R Console
# List all installed packages installed.packages() # Check if a specific package is installed "ggplot2" %in% rownames(installed.packages()) # [1] TRUE # Update all packages to latest versions update.packages()

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:

CRAN
The official R repository. Packages here are reviewed and tested. Use install.packages() to access them.
Bioconductor
A specialized repository for bioinformatics and genomics packages. Uses its own installer.
GitHub
Development versions and cutting-edge packages. Installed via the devtools or remotes package.

Common Mistakes

✕ Forgetting Quotes in install.packages()
Writing install.packages(ggplot2) without quotes causes an "object not found" error. Always use install.packages("ggplot2").
✕ Installing Every Session
Running install.packages() every time you start R wastes time. Install once, then just use library() each session.
✕ Forgetting to Load
Installing a package doesn't make it usable. You must call library() before using any of its functions.
✕ Misspelling Package Names
Package names are case-sensitive. "Ggplot2" won't work: it must be exactly "ggplot2" in lowercase.

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?