Installing R and RStudio

Overview

Before we can start working with the tidyverse, you’ll need R and RStudio installed on your own computer. R must be installed first — RStudio is an interface that runs on top of R, not a replacement for it.

Installation steps

1. Check your computer type

On Mac: Apple menu → About This Mac → check whether you have an Apple (M-series) chip or an Intel processor. This determines which R installer you need.

2. Download and install R first

Go to CRAN, choose your operating system, and download the base installer:

  • Windows: choose “base”
  • Mac: choose the installer matching the chip type found in Step 1

Run the installer with default settings. You won’t open R directly after this — it runs in the background for RStudio to use.

3. Download and install RStudio Desktop

Go to posit.co/download/rstudio-desktop. The page auto-detects your OS — download the free RStudio Desktop Open Source edition and run the installer with default settings.

4. Open RStudio and verify the setup

Launch RStudio (not R). You should see four panels. Click into the Console panel (bottom-left), type:

1 + 1

and press Enter. If it returns 2, R and RStudio are correctly linked.

5. Install the packages used in this course

The course materials use the tidyverse set of packages, plus gt (which also supplies some of the example datasets, such as gtcars), duckdb (the database we use for SQL) and reticulate (which runs Python inside RStudio). Install them once, from the RStudio Console:

install.packages(c("tidyverse", "gt", "gtExtras", "DBI", "duckdb", "reticulate"))

This takes a few minutes and prints a lot of output; that is normal. On some computers R has to build DuckDB from its source code, which can take 10 minutes or more. Let it finish.

6. Run the course setup chunks

Every page in the course starts with one or more course setup chunks: an R one, plus an SQL or Python one on those pages. Each checks that your R, packages and Python are new enough for the course, stops with instructions if anything needs fixing, and then loads what the page needs.

Check your whole setup now. Create a new Quarto document (File → New File → Quarto Document…), copy the four chunks below into it (the Copy chunk button on each copies the whole chunk, ready to paste), and run them in order with the green ▶ arrow on each chunk.

Course setup (R): check versions and load packages
# Checks that R and each package are installed and new enough for this course.
# Stops with a list of everything that needs fixing, and how to fix it.
check_course_versions <- function(required, area) {
  problems <- character(0)
  to_install <- character(0)
  found <- character(0)
  for (name in names(required)) {
    need <- required[[name]]
    if (name == "R") {
      have <- getRversion()
      if (have < need) {
        problems <- c(problems, sprintf(
          "R is version %s; this course needs %s or newer. Install the current R from https://cran.r-project.org/ (see https://howisonlab.github.io/datawrangling/current/rtidyverse/rstudio_installation.html), then reinstall your packages.",
          have, need))
      }
      found <- c(found, paste("R", have))
    } else if (!nzchar(system.file(package = name))) {
      problems <- c(problems, sprintf("%s is not installed.", name))
      to_install <- c(to_install, name)
    } else {
      have <- packageVersion(name)
      if (have < need) {
        problems <- c(problems, sprintf(
          "%s is version %s; this course needs %s or newer.", name, have, need))
        to_install <- c(to_install, name)
      } else if (isNamespaceLoaded(name) && getNamespaceVersion(name) != have) {
        problems <- c(problems, sprintf(
          "%s %s is installed, but this R session is still using %s. Restart R.",
          name, have, getNamespaceVersion(name)))
      }
      found <- c(found, paste(name, have))
    }
  }
  if (length(problems) > 0) {
    fix <- "In RStudio choose Session > Restart R"
    if (length(to_install) > 0) {
      fix <- paste0(fix, ", then run this in the Console:\n  install.packages(c(",
                    paste0('"', to_install, '"', collapse = ", "), "))\n")
    }
    stop("Course setup (", area, ") found problems:\n  - ",
         paste(problems, collapse = "\n  - "), "\n\n",
         fix, "\nThen run the setup chunk(s) again.", call. = FALSE)
  }
  cat("Course setup OK (", area, "): ", paste(found, collapse = ", "), "\n", sep = "")
}

check_course_versions(c(
  R = "4.5.0",         # CRAN builds Mac/Windows packages for the current and previous R only
  tidyverse = "2.0.0",
  dplyr = "1.2.0",     # recode_values() and replace_values()
  tidyr = "1.3.0",     # separate_wider_delim()
  gt = "1.0.0",        # needed by gtExtras
  gtExtras = "0.6.1"   # works with ggplot2 4
), "R")

library(tidyverse)
library(gt)
library(gtExtras)
Course setup OK (R): R 4.6.1, tidyverse 2.0.0, dplyr 1.2.1, tidyr 1.3.2, gt 1.3.0, gtExtras 0.6.2
Course setup (SQL): check versions and connect to DuckDB
# needs check_course_versions() from the Course setup (R) chunk
check_course_versions(c(
  DBI = "1.0.0",
  duckdb = "1.5.5"     # duckdb(shared_home = FALSE)
), "SQL")

library(DBI)
library(duckdb)
options(duckdb.enable_rstudio_connection_pane = TRUE)

# a temporary in-memory database, nothing saved to a file
drv <- duckdb(shared_home = FALSE)
con <- dbConnect(drv)
Course setup OK (SQL): DBI 1.3.0, duckdb 1.5.6
Course setup (Python): check reticulate and choose Python packages
# needs check_course_versions() from the Course setup (R) chunk
check_course_versions(c(
  reticulate = "1.41.0"  # py_require()
), "Python")

library(reticulate)
# reticulate sets up a Python with these packages for us (the first run downloads them).
# This must run before any python chunk: if Python has already started, restart R first.
py_require(c(
  "pandas>=2.0",         # pivot() with named arguments only
  "numpy",
  "pyjanitor"            # clean_names() for snake_case column names
), python_version = ">=3.11")
Course setup OK (Python): reticulate 1.46.0
Course setup (Python): import packages
import sys
import pandas as pd
import numpy as np
import janitor
from io import StringIO

print("Course setup OK (Python): Python", sys.version.split()[0], "pandas", pd.__version__)
Course setup OK (Python): Python 3.12.13 pandas 3.0.6

Each chunk should print a line starting Course setup OK. The Python chunks download Python and pandas the first time, which can take a minute or two.

If a chunk stops with “Course setup … found problems”, do what the message says, usually Session → Restart R, then an install.packages(...) line, then run the chunks again. If you get stuck, bring the message to class or office hours.

You only need to install.packages once per computer, but you copy the setup chunks to the top of every notebook you write for this course.

7. Handle Mac security warnings if needed

If macOS blocks RStudio with “cannot be opened because the developer cannot be verified,” go to System Preferences → Privacy & Security and click “Open Anyway” next to the RStudio message.

Troubleshooting

  • RStudio won’t open / no console appears: R was likely not installed, or was installed after RStudio. Reinstall R, then relaunch RStudio.
  • “Developer cannot be verified” (Mac): System Preferences → Privacy & Security → “Open Anyway.”
  • Very old operating system: current RStudio may not install. Archived older RStudio versions are available on the Posit downloads page.
  • Package installation errors later in the course: usually resolved by updating to the newest version of R via CRAN.

If you’re still stuck, bring your laptop to office hours or book a slot through the Canvas appointment scheduler.