Data and Code in Quarto

Code Chunks, Loading Data, and Inline Reporting

Daniela Palleschi

April 29, 2026

Overview

  • Code chunks: structure and options
  • Loading and saving data
  • Inline reporting

The dataset

We will use eye-tracking reading data from Palleschi et al. (2025), adapted for this workshop.

  • Design: 2 × 2 — lifetime (living/dead) × tense (present perfect/simple future)
  • Regions: verb of interest for today
  • Reading measures: gaze (first-pass reading time) for today
  • Additional: rating (1–7 plausibility), rt_ms (reaction time to rating)
  • Note: participant codes (px_code) are randomly generated; we will anonymise these later

Tip

The verb region is the most theoretically relevant. We will focus on gaze in most exercises.

Code chunks

Structure

## Reaction times

Calculate mean reaction times:

```{r}
rt <- c(312, 287, 445, 301, 389)
mean(rt)
```
  • Fenced with ```{r} and ```
  • Executed in sequence; output inserted at that position
  • Code must be written linearly: top to bottom

Chunk options

```{r}
#| echo: false
#| warning: false
5 * 6
```
  • must appear directly below ```{r}, and
  • be preceded with #| + an empty space, and
  • be followed by a colon + empty space + value
  • apply only to when rendering or ‘Run all chunks (above)’, not when running interactively
Option Effect
echo show or hide code
output show or hide output
eval run or skip chunk
include include chunk and output
warning show or hide warnings
message show or hide messages
error halt on error (false) or continue and display error (true)

Global defaults and overrides

Set globally in YAML:

execute:
  echo: false
  warning: false
  message: false

Override per chunk:

```{r}
#| eval: true
#| error: false
2+2
```
[1] 4

✏️ Exercises — 5 minutes

Exercises 1 - 3: Code chunks

Loading data

Packages

Always load pacakges at the top of your script!

```{r}
library(here)
library(tidyverse)
```

I like the pacman::p_load() function for loading packages instead.

# install.packages("pacman")
pacman::p_load(here, tidyverse)

here::here()

Always use here::here() for project-relative paths:

```{r}
df       <- readr::read_csv(here::here("data", "raw", "data_example.csv"))
fit      <- readRDS(here::here("output", "models", "lmer_gaze.rds"))
```

Save processed objects so the manuscript never re-runs the analysis:

```{r}
#| eval: false
readr::write_csv(df_clean, here::here("data", "processed", "data_clean.csv"))
```

Note

The analysis script processes/analyses data and saves it under a new filename.

The manuscript script loads in the processed/analysed data and models and reports them.

✏️ eval: true or false?

In an analysis script, which code chunks should be evaluated when rendering?

  • loading in your processed data
  • wrangling or checking distributions
  • running models
  • saving models as .rds files
  • re-loading in saved models
  • printing model summaries
  • running model diagnostics

Anonymising data

  • here we create an anonymised participant code and save the result:
# create anonymised participant codes
set.seed(416)

px_lookup <- df |>
  distinct(px_code) |> # for each distinct px_code
  mutate(px = sample(1:n(), n(), replace = FALSE)) # randomly assign a value of 1:totalN

df_clean <- df |>
  left_join(px_lookup, by = "px_code") |>
  select(-px_code)

Saving cleaned data

  • cleaning/processing usually includes a lot more than just anonymise your data
  • for now let’s leave it at that
```{r}
#| eval: false
saveRDS(here("data", "processed", "data_clean.csv"))
```
  • eval: false because we don’t want it to be saved with each render
  • usually at this point you would/should delete the raw data from your local machine
    • and archive and encrypt it somewhere secure
  • note that we saved it in a subfolder for processed data

✏️ Exercises — 5 minutes

Exercises 4 - 6: Load data

Inline reporting

The idea

Instead of typing numbers manually:

“The sample comprised 48 participants.”

Embed directly in prose:

The sample comprised ` r dplyr::n_distinct(df$px)` participants.

Re-render when data changes – numbers update automatically.

Store values as objects

The sample comprised ` r n_px` participants.
Mean total reading time was ` r m_tt` ms (*SD* = ` r sd_tt`).

The sample comprised 24 participants. Mean total reading time was 350 ms (SD = 56).

✏️ Exercises — 10 minutes

Exercises 7 - 9

Summary

  • Chunk options control what is shown and what runs
  • here::here() makes paths portable — the manuscript only loads, never re-runs
  • Inline R keeps reported values in sync with your analysis
  • Any number in your manuscript should be computed, not typed

Up next…

Reporting models

Analyses and manuscripts in Quarto

References

Palleschi, D., Ronderos, C. R., & Knoeferle, P. (2025). Living in the present – how referent lifetime influences processing of past, present (perfect), and future tenses. Glossa Psycholinguistics, 4(1), 1–48. https://doi.org/https://doi.org/10.5070/G601119481