The R Workflow

Environment, Dependencies, and Data Foundations

Today’s Goal

Get everyone set up with a working R environment and understand how the pieces fit together.

Then: hands-on practice in the notebook.

The Stack

Your Code
    ↓
R Packages (dplyr, ggplot2, HADES...)
    ↓
R (the interpreter)
    ↓
System Dependencies (Java, compilers...)
    ↓
Operating System (Windows, macOS, Linux)

R Sessions and Objects

When R starts, it creates a session with:

  • A global environment (where your objects live)
  • A working directory (where R looks for files)
  • Loaded packages (base R + what you load)

Everything you create is an object. Even functions are objects.

Assignment and Scope

x <- 1

add_to_self <- function(y) {
  x <- y       # Local assignment
  y + y
}

add_to_self(3)  # returns 6
x               # still 1 (unchanged!)

Assignments inside a function stay local. Functions don’t modify objects in place—you must capture return values.

Environments

  • Global environment: where your top-level objects live
  • Function environments: created when a function runs, destroyed when it returns
  • R packages are bundles of functions in their own environments

This isolation is why R is safe for exploratory analysis.

Packages

A package bundles functions, documentation, and data.

Source Examples Install command
CRAN dplyr, ggplot2 install.packages()
Bioconductor DESeq2 BiocManager::install()
GitHub OHDSI/HADES remotes::install_github()

Installing = download to disk (once)

Loading = make available in session (library() each time)

Next

Open the notebook and let’s work through it together.

modules/01_r-workflow/r-foundations.qmd