Skip to content

R - Packages

R’s power comes from its vast ecosystem of packages: collections of functions, data, and compiled code that extend R’s capabilities. These packages are stored in a directory called a “library”. While base R is powerful, almost any real-world project will rely on external packages.

The primary repository for R packages is the Comprehensive R Archive Network (CRAN). You can easily install and load packages from CRAN.

# 1. Install a package from CRAN (you only need to do this once)
# Let's install 'dplyr', a core package for data manipulation.
install.packages("dplyr")
# You can also install multiple packages at once
install.packages(c("ggplot2", "readr"))
# 2. Load the package into your R session to use its functions
# You must do this every time you start a new R session.
library(dplyr)
# Now you can use functions from the dplyr package, like glimpse()
# (using the built-in 'mtcars' dataset as an example)
data(mtcars)
glimpse(mtcars)

You can inspect your package setup with a few simple commands.

# See which packages are currently loaded in your session
search()
# Get the file path(s) where your packages are stored
.libPaths()
# See all installed packages in your library (this can be a long list!)
# In RStudio, the 'Packages' pane is a more user-friendly way to browse.
# installed.packages()

A common problem in data science is reproducibility. If you share your code with a colleague or return to it a year later, you need to ensure it still runs correctly with the right package versions. The modern solution for this in R is the renv package, which creates project-specific, isolated libraries.

Here is the best-practice workflow for starting a new, reproducible project:

  1. Create an RStudio Project: Go to File > New Project... > New Directory > New Project. This creates a self-contained folder for all your project files.
  2. Initialize renv: In the console, run renv::init(). This creates a project-local library and a renv.lock file to track package versions.
  3. Install and Work: Install the packages you need (install.packages("tidyverse"), etc.). renv will install them into the project’s private library.
  4. Save the State: When you’ve installed or updated packages, run renv::snapshot(). This updates the renv.lock file with the exact versions you used.
  5. Share: When you share the project folder, others can run renv::restore() to install the exact same package versions, guaranteeing the code will run identically.
# Step 2: Initialize renv in your new RStudio Project
# renv::init()
# Step 3: Install packages as needed
# install.packages("remotes")
# Step 4: Save the state of your library to the lockfile
# renv::snapshot()
# Step 5: If someone else opens your project, they run this to get the same packages
# renv::restore()

Instead of installing packages one-by-one, it’s often more efficient to install curated collections. The most important of these is the tidyverse.

  • tidyverse: A meta-package that installs a suite of essential data science packages with a shared philosophy and consistent syntax. It includes dplyr (data manipulation), ggplot2 (visualization), readr (data import), tidyr (data tidying), stringr (strings), and more.
  • tidymodels: A meta-package for modeling and machine learning, providing a unified and modern framework for the entire modeling pipeline.
# Install the entire tidyverse collection
install.packages("tidyverse")
# Load the core tidyverse packages
library(tidyverse)

Best Practice: For any new analysis, start by creating an RStudio Project and using renv for dependency management. This will make your work more professional, portable, and reproducible.