
Writing R Packages
Grayson White
Math 241
Week 11 | Spring 2026

Monday Lecture:
Wednesday Lecture:
R packagesHow do you share code?
R scripts and Quarto documentsHow to pick between the options?
R PackageWhat is an R package?
“R packages are the fundamental unit of R-ness”. – Jenny Bryan
Contains (usually) functions, (possibly) datasets, documentation, and tests.
“base R”: a handful of base packages that are preloaded.
CRAN has > 23,000 more packages
install.packages("dplyr")library(dplyr)And then there are all the packages on GitHub:
devtools::install_github("hadley/genzplyr")library(genzplyr)library(mosaicData)data(package = "mosaicData")?Births2015library(pdxTrees)get_pdxTrees_parks()get_pdxTrees_streets()library(gbfs)R folder: Codetests folder: Testing functionsdata folder: Storing dataKey packages:
R Package!pdxHolesGoals:
pdxHoles. Useful MVP code:library(tidyverse)
library(sf)
potholes <- st_read("data/potholes.geojson", quiet = TRUE) |>
select(-OBJECTID, -ITEM_CATEGORY_NAME) |>
rename(
id = ITEM_ID,
status = ITEM_STATUS,
date_reported = ITEM_DATE_CREATED,
neighborhood = LOCATION_NEIGHBORHOOD
) |>
mutate(
date_reported = as_datetime(date_reported / 1000, tz = "America/Los_Angeles"),
neighborhood = str_to_title(neighborhood),
)
plot_holes <- function(colors = c("olivedrab", "darkgoldenrod", "red3"),
alpha = 1) {
ggplot(data = potholes,
mapping = aes(color = status)) +
geom_sf(alpha = alpha) +
scale_color_manual(values = colors) +
theme_bw()
}pdxHoles. Useful MVP code:Let’s go through the “Creating an R Package” hand-out.
I will demo the process with the PDX pothole data.
If following along, clone this repo: https://github.com/Reed-Data-Science/pkgDemo
Most common confusions when moving from R scripts/Quarto docs to writing R packages:
DESCRIPTION file to declare dependencies.package_name::function_name().library(package_name).Other questions?
R packages!