+ - 0:00:00
Notes for current slide
Notes for next slide

AdvR Chapter 22: Debugging

Tan Ho (@_TanHo)

Last Updated: 2020-08-18

1 / 14

Preface

2 / 14

Preface

Everything I Know Is From Jenny Bryan

"Everything I Know Is From Jenny Bryan"

- Sharla Gelfand

3 / 14

Preface

Everything I Know Is From Jenny Bryan

"Everything I Know Is From Jenny Bryan"

- Sharla Gelfand

- Tan Ho

4 / 14

Preface

There are some excellent talks on debugging (some are better on debugging than Adv R itself!)

5 / 14

Preface

There are some excellent talks on debugging (some are better on debugging than Adv R itself!)

6 / 14

Preface

There are some excellent talks on debugging (some are better on debugging than Adv R itself!)

and I suggest that you visit these resources later if you're looking for more of a talk or text-based content on debugging.

7 / 14

Today's Plan

We've all "read" the chapter, so I'll (obviously) take the liberty to live-code some debugging. Along the way, we'll try to hit:

  • traceback
  • print debugging
  • browser()
  • debug/debugonce/undebug
  • options(error = recover)

by looking through some examples, a few contrived, a few less-contrived, and lastly (possibly?) a Shiny app?

8 / 14

Examples, easy mode

Let's use one of the canned examples from rstats.wtf to get us started.

usethis::use_course("rstd.io/wtf-debugging")
# Example 1: Spartan
9 / 14

Examples, AdvR mode

Maya: "How do I debug this"?

10 / 14

Examples, AdvR mode

Maya: "How do I debug this"? (SURPRISE, METAPROGRAMMING REVIEW!)

11 / 14

Examples, AdvR mode

Maya: "How do I debug this"? (SURPRISE, METAPROGRAMMING REVIEW!)

Advanced R Slack Debug

12 / 14

Does anyone remember what this problem was about?

subset2 <- function(data, rows) {
rows <- rlang::enquo(rows)
# change val from 2 to 3, breaking things
rlang::env_bind(rlang::caller_env(), val = 3)
rows_val <- rlang::eval_tidy(rows, data)
stopifnot(is.logical(rows_val))
data[rows_val, , drop = FALSE]
}
resample <- function(df, n) {
idx <- sample(nrow(df), n, replace = TRUE)
df[idx, , drop = FALSE]
}
subsample <- function(df, cond, n = nrow(df)) {
df <- subset2(df, cond)
resample(df, n)
}
df <- data.frame(x = c(1,1,1,2,2), y = 1:5)
subsample(df, x == 1)
# Error in rlang::eval_tidy(rows, data) : object 'x' not found
13 / 14

Debug a Shiny app?

I can do this too if people are keen!

14 / 14

Preface

2 / 14
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
oTile View: Overview of Slides
Esc Back to slideshow