Lab Setup: R Packages and Build Tools

Set up your computer before the lab. Package installation can take time, and some errors require system tools that cannot be fixed quickly during class.

What You Need

You need five things, in this order:

  1. R and RStudio.
  2. System build tools for your operating system.
  3. Quarto's TinyTeX tool for PDF rendering.
  4. Core course packages from CRAN.
  5. Course packages from GitHub.

Restart RStudio after installing system build tools or GitHub packages.

Step 1: Install R and RStudio

Install R first, then install RStudio.

Use the current release unless you have been told otherwise.

Step 2: Install System Build Tools

Some R packages need to compile code. If your error mentions make, gcc, g++, clang, or compilation, this step is probably missing.

macOS

Install Xcode Command Line Tools (Xcode CLT). In Terminal, run:

xcode-select --install

Follow the prompts, then restart RStudio. You do not need the full Xcode app.

Windows

Install Rtools for your version of R:

https://cran.r-project.org/bin/windows/Rtools/

Restart RStudio, then check:

Sys.which("make")

If this returns a path, R can find the build tools.

Linux

Install the usual build tools. On Ubuntu or Debian:

sudo apt install build-essential

Restart RStudio afterwards.

Step 3: Install TinyTeX for Quarto PDFs

The final report must render to PDF. Installing the R package called tinytex is not enough: Quarto also needs a TeX distribution. Quarto recommends TinyTeX for PDF documents, installed from the system command line.

Run this in a terminal, not in the R console:

quarto install tinytex

Then check that Quarto can see it:

quarto list tools

If TinyTeX is already installed but Quarto says it is out of date, run:

quarto update tinytex

macOS

Open Terminal (or the RStudio Terminal tab) and run:

quarto install tinytex
quarto list tools

If quarto is not found, install or update Quarto from https://quarto.org/docs/download/.

Windows

Open Command Prompt, PowerShell, or the Terminal tab in RStudio. Run:

quarto install tinytex
quarto list tools

Do not run this command in the R console. If Windows says quarto is not recognised, install Quarto from https://quarto.org/docs/download/, close and reopen RStudio, then try again. If the PDF render later says lualatex or a LaTeX package is missing, rerun quarto install tinytex and render again.

Linux

Run the same command in your terminal:

quarto install tinytex

A system TeX Live installation can also work, but TinyTeX is the supported route for this course.

Step 4: Install CRAN Packages

Run this in R:

cran_packages <- c(
  "ggplot2", "dplyr", "tibble", "tidyr", "purrr", "grf",
  "arrow", "qs2", "googledrive", "pak", "rmarkdown", "knitr",
  "kableExtra", "tinytex", "ggdag", "dagitty"
)

missing_cran <- cran_packages[
  !vapply(cran_packages, \(p) requireNamespace(p, quietly = TRUE), logical(1))
]

if (length(missing_cran) > 0) {
  install.packages(missing_cran)
}

Step 5: Install GitHub Packages

Use pak first. It is the recommended installer because it resolves dependencies well and gives useful diagnostics. It still needs the system build tools above when packages must be built from source.

pak::pak("go-bayes/margot")
pak::pak("go-bayes/causalworkshop")
pak::pak("go-bayes/boilerplate")

Fallback: remotes

If pak itself fails, try remotes:

install.packages("remotes")
remotes::install_github("go-bayes/margot", upgrade = "never")
remotes::install_github("go-bayes/causalworkshop", upgrade = "never")
remotes::install_github("go-bayes/boilerplate", upgrade = "never")

If the error mentions make, gcc, g++, clang, or compilation, changing installers will not solve the problem. Install the system build tools in Step 2, restart RStudio, then rerun the pak commands.

Step 6: Check the Installation

Restart RStudio, then run:

packages <- c(
  "ggplot2", "dplyr", "tibble", "arrow", "qs2", "googledrive",
  "grf", "margot", "causalworkshop", "boilerplate"
)

status <- vapply(packages, requireNamespace, quietly = TRUE, logical(1))
print(status)

Every entry should be TRUE.

Check the course package version:

packageVersion("causalworkshop")

You need version 0.6.0 or later for Labs 8-10.

If Installation Fails

Read the first real error message, not only the final line. Common meanings:

  • quarto: command not found or 'quarto' is not recognized: install Quarto, restart RStudio, and run quarto install tinytex from a terminal.
  • lualatex: command not found, pdflatex: command not found, or missing LaTeX packages while rendering PDF: run quarto install tinytex from a terminal, then render again.
  • make: command not found: install Xcode CLT on macOS or Rtools on Windows.
  • gcc, g++, or clang missing: install system build tools.
  • cannot open URL or timeout: check the internet connection, then try again.
  • causalworkshop was upgraded while loaded: restart RStudio, then rerun the lab.

If the GitHub package installation still fails, use the course lab machine or the pre-installed lab environment. Do not spend class time debugging compilers.