Skip to content

Commit 3fe5d44

Browse files
Merge pull request #2 from rstats-wtf/update-examples
revise workshop examples
2 parents e2bdd9d + 97dce4d commit 3fe5d44

23 files changed

+713
-195
lines changed

01_debugging_comfy.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

01_debugging_jim.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

01_debugging_spartan.R

Lines changed: 0 additions & 30 deletions
This file was deleted.

01_exercise/01_debugging_comfy.R

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Origin -----------------------------------------------------------------------
2+
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
3+
# Object of type 'closure' is not subsettable,
4+
# https://github.com/jennybc/debugging
5+
6+
7+
# open 01_sourceme to inspect & review code ---------------------------------------
8+
# try file.edit()
9+
10+
11+
# source functions -------------------------------------------------------------
12+
# source in 01_exercise/01_sourceme.R
13+
14+
# view fruit data --------------------------------------------------------------
15+
16+
# successful execution ---------------------------------------------------------
17+
# confirm function works for berry
18+
fruit_avg(fruit, "berry")
19+
20+
# error on execution -----------------------------------------------------------
21+
# observe error
22+
fruit_avg(fruit, "peach")
23+
24+
# identify location of error ---------------------------------------------------
25+
# try traceback()
26+
27+
# copy and paste traceback results as a comment here to compare with next exercise
28+
29+
30+
# modify options for a richer traceback via rlang ------------------------------
31+
32+
# copy and paste traceback results as a comment here to compare with next exercise
33+
34+
35+
# interactive debugging --------------------------------------------------------
36+
# enter interactive debugger by inserting browser() into the fruit_avg() function
37+
# trigger interactive debugger by executing function
38+
39+
# use these commands at the Browse[]> prompt to navigate the debugger
40+
41+
# | command | operation |
42+
# |---------|-------------------------|
43+
# | `n` | next statement |
44+
# | `c` | continue |
45+
# | `s` | step into function call |
46+
# | `f` | finish loop / function |
47+
# | `where` | show previous calls |
48+
# | `Q` | quit debugger |
49+
50+
# while in the debugger, explore the objects in your environment
51+
# with ls.str()
52+
53+
# Can you determine exactly why the bug is occurring?
54+
# If you have time, try to fix it!
55+
56+
57+
58+

01_exercise/01_debugging_solution.R

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Origin -----------------------------------------------------------------------
2+
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
3+
# Object of type 'closure' is not subsettable,
4+
# https://github.com/jennybc/debugging
5+
6+
7+
# open sourceme to inspect & review code ---------------------------------------
8+
file.edit("01_exercise/01_sourceme.R")
9+
10+
11+
# source functions -------------------------------------------------------------
12+
# open 01_exercise/01_sourceme.R and click on Source button
13+
# OR
14+
source("01_exercise/01_sourceme.R")
15+
16+
17+
# view fruit data --------------------------------------------------------------
18+
fruit
19+
20+
21+
# successful execution ---------------------------------------------------------
22+
# confirm function works for berry
23+
fruit_avg(fruit, "berry")
24+
25+
# error on execution -----------------------------------------------------------
26+
# observe error
27+
fruit_avg(fruit, "peach")
28+
29+
# identify location of error ---------------------------------------------------
30+
traceback()
31+
32+
# copy and paste traceback results as a comment here to compare with next exercise
33+
34+
35+
# modify options for a richer traceback ----------------------------------------
36+
options(error = rlang::entrace)
37+
fruit_avg(fruit, "peach")
38+
rlang::last_error()
39+
rlang::last_trace()
40+
41+
# copy and paste traceback results as a comment here to compare with next exercise
42+
43+
44+
# interactive debugging --------------------------------------------------------
45+
# enter interactive debugger by inserting `browser()` statement in
46+
# the fruit_avg() function in 01_sourceme.R
47+
# Restart R, re-source file
48+
source("01_exercise/01_sourceme.R")
49+
50+
# trigger interactive debugger
51+
fruit_avg(fruit, "peach")
52+
53+
# use these commands at the Browse[]> prompt to navigate the debugger
54+
55+
# | command | operation |
56+
# |---------|-------------------------|
57+
# | `n` | next statement |
58+
# | `c` | continue |
59+
# | `s` | step into function call |
60+
# | `f` | finish loop / function |
61+
# | `where` | show previous calls |
62+
# | `Q` | quit debugger |
63+
64+
# while in the debugger, explore the objects in your environment
65+
# with ls.str()
66+
67+
# Can you determine exactly why the bug is occurring?
68+
# If you have time, try to fix it!
69+
70+

01_exercise/01_debugging_spartan.R

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Origin -----------------------------------------------------------------------
2+
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
3+
# Object of type 'closure' is not subsettable,
4+
# https://github.com/jennybc/debugging
5+
6+
7+
# open 01_sourceme to inspect & review code ---------------------------------------
8+
9+
10+
# source functions -------------------------------------------------------------
11+
12+
13+
# view fruit data --------------------------------------------------------------
14+
15+
16+
# successful execution ---------------------------------------------------------
17+
# confirm function works for berry
18+
fruit_avg(fruit, "berry")
19+
20+
# error on execution -----------------------------------------------------------
21+
# observe error
22+
fruit_avg(fruit, "peach")
23+
24+
25+
26+
# identify location of error ---------------------------------------------------
27+
28+
# copy and paste traceback results as a comment here to compare with next exercise
29+
30+
31+
32+
# modify options for a richer traceback ----------------------------------------
33+
34+
35+
# copy and paste traceback results as a comment here to compare with next exercise
36+
37+
38+
# interactive debugging --------------------------------------------------------
39+
# enter interactive debugger
40+
41+
# Can you determine exactly why the bug is occurring?
42+
# If you have time, try to fix it!
43+

01_exercise/01_sourceme.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
fruit <- data.frame(
2+
row.names = c("calories", "weight", "yumminess"),
3+
blackberry = c(4L, 9L, 6L),
4+
blueberry = c(1L, 2L, 8L),
5+
peach = c(59L, 150L, 10L),
6+
plum = c(30L, 78L, 5L)
7+
)
8+
9+
fruit_avg <- function(dat, pattern) {
10+
cols <- grep(pattern, names(dat))
11+
mini_dat <- dat[ , cols]
12+
message("Found ", ncol(mini_dat), " fruits!")
13+
rowMeans(mini_dat)
14+
}

02_debugging_comfy.R

Lines changed: 0 additions & 17 deletions
This file was deleted.

02_debugging_jim.R

Lines changed: 0 additions & 26 deletions
This file was deleted.

02_debugging_spartan.R

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)