Skip to content

Commit

Permalink
Merge pull request #2 from rstats-wtf/update-examples
Browse files Browse the repository at this point in the history
revise workshop examples
  • Loading branch information
shannonpileggi authored Jul 2, 2022
2 parents e2bdd9d + 97dce4d commit 3fe5d44
Show file tree
Hide file tree
Showing 23 changed files with 713 additions and 195 deletions.
30 changes: 0 additions & 30 deletions 01_debugging_comfy.R

This file was deleted.

30 changes: 0 additions & 30 deletions 01_debugging_jim.R

This file was deleted.

30 changes: 0 additions & 30 deletions 01_debugging_spartan.R

This file was deleted.

58 changes: 58 additions & 0 deletions 01_exercise/01_debugging_comfy.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Origin -----------------------------------------------------------------------
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
# Object of type 'closure' is not subsettable,
# https://github.com/jennybc/debugging


# open 01_sourceme to inspect & review code ---------------------------------------
# try file.edit()


# source functions -------------------------------------------------------------
# source in 01_exercise/01_sourceme.R

# view fruit data --------------------------------------------------------------

# successful execution ---------------------------------------------------------
# confirm function works for berry
fruit_avg(fruit, "berry")

# error on execution -----------------------------------------------------------
# observe error
fruit_avg(fruit, "peach")

# identify location of error ---------------------------------------------------
# try traceback()

# copy and paste traceback results as a comment here to compare with next exercise


# modify options for a richer traceback via rlang ------------------------------

# copy and paste traceback results as a comment here to compare with next exercise


# interactive debugging --------------------------------------------------------
# enter interactive debugger by inserting browser() into the fruit_avg() function
# trigger interactive debugger by executing function

# use these commands at the Browse[]> prompt to navigate the debugger

# | command | operation |
# |---------|-------------------------|
# | `n` | next statement |
# | `c` | continue |
# | `s` | step into function call |
# | `f` | finish loop / function |
# | `where` | show previous calls |
# | `Q` | quit debugger |

# while in the debugger, explore the objects in your environment
# with ls.str()

# Can you determine exactly why the bug is occurring?
# If you have time, try to fix it!




70 changes: 70 additions & 0 deletions 01_exercise/01_debugging_solution.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Origin -----------------------------------------------------------------------
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
# Object of type 'closure' is not subsettable,
# https://github.com/jennybc/debugging


# open sourceme to inspect & review code ---------------------------------------
file.edit("01_exercise/01_sourceme.R")


# source functions -------------------------------------------------------------
# open 01_exercise/01_sourceme.R and click on Source button
# OR
source("01_exercise/01_sourceme.R")


# view fruit data --------------------------------------------------------------
fruit


# successful execution ---------------------------------------------------------
# confirm function works for berry
fruit_avg(fruit, "berry")

# error on execution -----------------------------------------------------------
# observe error
fruit_avg(fruit, "peach")

# identify location of error ---------------------------------------------------
traceback()

# copy and paste traceback results as a comment here to compare with next exercise


# modify options for a richer traceback ----------------------------------------
options(error = rlang::entrace)
fruit_avg(fruit, "peach")
rlang::last_error()
rlang::last_trace()

# copy and paste traceback results as a comment here to compare with next exercise


# interactive debugging --------------------------------------------------------
# enter interactive debugger by inserting `browser()` statement in
# the fruit_avg() function in 01_sourceme.R
# Restart R, re-source file
source("01_exercise/01_sourceme.R")

# trigger interactive debugger
fruit_avg(fruit, "peach")

# use these commands at the Browse[]> prompt to navigate the debugger

# | command | operation |
# |---------|-------------------------|
# | `n` | next statement |
# | `c` | continue |
# | `s` | step into function call |
# | `f` | finish loop / function |
# | `where` | show previous calls |
# | `Q` | quit debugger |

# while in the debugger, explore the objects in your environment
# with ls.str()

# Can you determine exactly why the bug is occurring?
# If you have time, try to fix it!


43 changes: 43 additions & 0 deletions 01_exercise/01_debugging_spartan.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Origin -----------------------------------------------------------------------
# This example was originally used inJenny Bryan's rstudio::conf(2020) keynote
# Object of type 'closure' is not subsettable,
# https://github.com/jennybc/debugging


# open 01_sourceme to inspect & review code ---------------------------------------


# source functions -------------------------------------------------------------


# view fruit data --------------------------------------------------------------


# successful execution ---------------------------------------------------------
# confirm function works for berry
fruit_avg(fruit, "berry")

# error on execution -----------------------------------------------------------
# observe error
fruit_avg(fruit, "peach")



# identify location of error ---------------------------------------------------

# copy and paste traceback results as a comment here to compare with next exercise



# modify options for a richer traceback ----------------------------------------


# copy and paste traceback results as a comment here to compare with next exercise


# interactive debugging --------------------------------------------------------
# enter interactive debugger

# Can you determine exactly why the bug is occurring?
# If you have time, try to fix it!

14 changes: 14 additions & 0 deletions 01_exercise/01_sourceme.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
fruit <- data.frame(
row.names = c("calories", "weight", "yumminess"),
blackberry = c(4L, 9L, 6L),
blueberry = c(1L, 2L, 8L),
peach = c(59L, 150L, 10L),
plum = c(30L, 78L, 5L)
)

fruit_avg <- function(dat, pattern) {
cols <- grep(pattern, names(dat))
mini_dat <- dat[ , cols]
message("Found ", ncol(mini_dat), " fruits!")
rowMeans(mini_dat)
}
17 changes: 0 additions & 17 deletions 02_debugging_comfy.R

This file was deleted.

26 changes: 0 additions & 26 deletions 02_debugging_jim.R

This file was deleted.

9 changes: 0 additions & 9 deletions 02_debugging_spartan.R

This file was deleted.

Loading

0 comments on commit 3fe5d44

Please sign in to comment.