R - Packages
R - Packages and Project Management
Section titled “R - Packages and Project Management”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.
Finding, Installing, and Loading Packages
Section titled “Finding, Installing, and Loading 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 onceinstall.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)Checking Your Environment
Section titled “Checking Your Environment”You can inspect your package setup with a few simple commands.
# See which packages are currently loaded in your sessionsearch()
# 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()Modern Project Management with renv
Section titled “Modern Project Management with renv”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.
A Reproducible Workflow
Section titled “A Reproducible Workflow”Here is the best-practice workflow for starting a new, reproducible project:
- Create an RStudio Project: Go to
File > New Project... > New Directory > New Project. This creates a self-contained folder for all your project files. - Initialize
renv: In the console, runrenv::init(). This creates a project-local library and arenv.lockfile to track package versions. - Install and Work: Install the packages you need (
install.packages("tidyverse"), etc.).renvwill install them into the project’s private library. - Save the State: When you’ve installed or updated packages, run
renv::snapshot(). This updates therenv.lockfile with the exact versions you used. - 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()Core Package Ecosystems
Section titled “Core Package Ecosystems”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 includesdplyr(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 collectioninstall.packages("tidyverse")
# Load the core tidyverse packageslibrary(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.