EDS 221 Day 9 AM

Troubleshooting


August 20th, 2026

So many functions!


How do I remember:

  • ymd() vs mdy()?
  • fct_reorder() vs fct_relevel()?
  • pivot_wider() vs pivot_longer()?

Posit Cheatsheets


https://rstudio.github.io/cheatsheets/

Posit Cheatsheets: lubridate


https://rstudio.github.io/cheatsheets/html/lubridate.html

Posit Cheatsheets: forcats


https://rstudio.github.io/cheatsheets/html/factors.html

Let’s talk about bugs


From the National Museum of American History (1994.0191.01)

Hitting a bug feels like hitting an iceberg


You’ll feel like panicking


But stay calm - you got this!


Where do bugs come from?


Bugs originate from errors in mental models (Ko and Myers 2005)

What kinds of mental models have you developed in this course?

Bugs are your friends


When you encounter a bug, the trivial goal is to fix the bug itself.

The more important goal is to use the bug to diagnose the error in your mental model.

Bugs are your friends! They teach you to be better programmers!

Take a systematic approach


1. Observe

Look closely at the code and the outputs. Make a list of your assumptions about how the code works.

2. Hypothesize

Analyze your assumptions. One of them probably has an error! Which ones seem closest to the source of the bug?

3. Test

Pick one assumption and test it.

4. Proceed

Continue testing, and ruling out, assumptions until you figure out the root cause.

A bug, in the wild


Let’s apply the loop to a real bug from the coral reef data.

library(tidyverse)

moorea_coral <- read_csv("data/moorea_coral.csv")

distinct_corals <- unique(moorea_coral$Taxonomy_Substrate_or_Functional_Group)
distinct_corals[-1:4]
Error in `distinct_corals[-1:4]`:
! only 0's may be mixed with negative subscripts

Step 1: Observe


library(tidyverse)

moorea_coral <- read_csv("data/moorea_coral.csv")

distinct_corals <- unique(moorea_coral$Taxonomy_Substrate_or_Functional_Group)
distinct_corals[-1:4]

List assumptions about how the code works.

  • moorea_coral is a data frame
  • It has a column called Taxonomy_Substrate_or_Functional_Group
  • unique() returns a character vector
  • distinct_corals has more than 4 values in it
  • -1:4 evaluates to -1, -2, -3, -4
  • [-1:4] drops the first four elements from a vector
  • The result is the unique “corals” in the dataset (except the first four)

Step 2: Hypothesize


The error message says:

Error in `distinct_corals[-1:4]`:
! only 0's may be mixed with negative subscripts

That leads me to believe the error is in the indexing. So I will prioritize these assumptions:

  • distinct_corals has more than 4 values in it
  • -1:4 evaluates to -1, -2, -3, -4
  • [-1:4] drops the first four elements from a vector

Step 3: Test


Begin with the assumption:

  • distinct_corals has more than 4 values in it

I’ll use the length function to test that assumption.

length(distinct_corals)
[1] 37

This assumption holds up - it’s not the source of the bug.

Step 4: Proceed


Move on to the next assumption:

  • -1:4 evaluates to -1, -2, -3, -4

Let’s check it out.

-1:4
[1] -1  0  1  2  3  4

This assumption fails - revealing the error in my mental model. Now I can fix both the bug and my understanding of sequences.

Your turn: find the bugs


There are 8 bugs scattered around the room.

Form eight groups, one at each bug.

Round 1: Brainstorm


For 5 minutes, brainstorm assumptions about how the code works.

Write down as many assumptions as you can, one per sticky note.

Round 2: Prioritize


Rotate to the next bug.

  1. Go through the previous group’s brainstorm.
  2. Pick the two assumptions you think are most likely related to the source of the bug.
  3. Move those two sticky notes into the boxes above.

Round 3: Test


Rotate to the next bug.

  1. Pick one of the prioritized assumptions from the previous group.
  2. Describe a test for that assumption.

Round 4: Test


Rotate one more time.

Describe a test for the last remaining assumption.

When the loop stalls: make a reprex


Sometimes you work through Observe -> Hypothesize -> Test -> Proceed and you’re still stuck. Time to ask for help.

A reprex (“reproducible example”) is a small, self-contained snippet that lets someone else run into your exact bug.

Building one is a debugging tool in itself: about 80% of the time, making a good reprex reveals the source of the problem before anyone else even looks at it.

A good reprex is…


Reproducible

Include everything needed to run it: library() calls, any data, and the code itself. Someone else should be able to copy-paste and get your exact error.

Minimal

Strip away everything unrelated to the bug. Use the smallest, simplest data that still triggers the problem.

Self-contained

Don’t rely on objects that only exist in your environment. Build any data the code needs within the reprex, before the problem code runs.

Making a reprex


  1. Write the smallest version of your code that still reproduces the bug.
  2. Copy it to your clipboard.
  3. Run:
reprex::reprex()
  1. reprex() runs your code in a fresh R session and copies a nicely formatted, ready-to-paste version — including output and errors — back to your clipboard.
  2. Paste it wherever you’re asking for help (Slack, GitHub issue, etc).

Before and after


❌ Not a reprex

“My mean() isn’t working, it just gives me NA, please help!!”

No code, no data, no error message — nobody can reproduce this.

✅ A reprex

y <- c(1, 2, NA, 4)
mean(y)
#> [1] NA

Runnable, minimal, and shows exactly what’s happening.

Ko, Amy J., and Brad A. Myers. 2005. “A Framework and Methodology for Studying the Causes of Software Errors in Programming Systems.” Journal of Visual Languages & Computing 16 (1-2): 41–84. https://doi.org/10.1016/j.jvlc.2004.08.003.