Skip to content

Commit 97d4139

Browse files
so close!
1 parent c2418ad commit 97d4139

8 files changed

+263
-67
lines changed

Diff for: demo/error_recover.R

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
source("demo/my_functions.R")
2+
options(error = recover)
3+
g("a")
4+
options(error = NULL)
5+
6+

Diff for: demo/warning.R

+9-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,9 @@
1-
options(warn = 0)
1+
options(warn = 0) # default, stores warnings until top-level function returns
2+
options(warn = 1) # warnings are printed as they occur
3+
options(warn = 2) # upgrades warnings to errors
4+
5+
6+
# initiate recover on warning
7+
options(warn = 2, error = recover)
8+
# restore original settings
9+
options(warn = 0, error = NULL)

Diff for: img/butterflies.gif

1.7 MB
Loading

Diff for: img/options-error-recover.PNG

25.8 KB
Loading

Diff for: img/swat-bugs.gif

3.07 MB
Loading

Diff for: img/traceback-ordering.PNG

32.2 KB
Loading

Diff for: index.html

+119-35
Large diffs are not rendered by default.

Diff for: index.qmd

+129-31
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,14 @@ Troubleshooting strategies:
199199

200200
3. Reprex
201201

202+
## But what if
203+
204+
![](img/swat-bugs.gif)
205+
206+
:::footer
207+
<https://giphy.com/gifs/TreehouseDirect-agent-binky-8UabbehIqkR0akDt1y>
208+
:::
209+
202210
# Moving on
203211

204212
<br>
@@ -213,6 +221,7 @@ from troubleshooting to...
213221
formal `debugging` techniques.
214222
:::
215223

224+
216225
## Key concepts
217226

218227
<br>
@@ -360,7 +369,7 @@ used interactively in console, called for last error observed
360369
the trace back is also known as: <br> call stack, stack trace, & back trace
361370
:::
362371

363-
## Richer trace back
372+
## Richer traceback
364373

365374
<br>
366375

@@ -373,7 +382,7 @@ rlang::last_error()
373382
374383
![](img/rlang_last-error.PNG)
375384
376-
## Richer trace back
385+
## Richer traceback
377386
378387
<br>
379388
@@ -391,6 +400,21 @@ rlang::last_trace()
391400

392401
`options(error = rlang::entrace)` <br> could go in your `.Rprofile`
393402

403+
## traceback vs rlang functions
404+
405+
<br>
406+
407+
Numbering and ordering differs between `traceback()` and rlang functions.
408+
409+
<brshort>
410+
411+
![](img/traceback-ordering.PNG)
412+
413+
414+
:::footer
415+
<https://adv-r.hadley.nz/debugging.html#call-stack-ordering>
416+
:::
417+
394418
## browser()
395419

396420
```{r, eval=FALSE, echo=TRUE, `code-line-numbers`="2"}
@@ -558,23 +582,50 @@ g("a")
558582

559583
- interactive debugger initiated a **single** time when `g()` is executed
560584

585+
## options(error = recover)
586+
587+
`r fontawesome::fa("check", fill = fa_fill)` we already discussed <br>
588+
`options(error = rlang::entrace)`<br>
589+
for a richer traceback on error
590+
591+
<br>
592+
593+
🤠 `options(error = recover)`
594+
595+
1. displays an interactive prompt with the traceback
596+
597+
2. allows you to select the frame for entering the debugger
598+
599+
600+
## recover example
601+
602+
```{r, eval=FALSE, echo=TRUE,`code-line-numbers`="2"}
603+
source("demo/my_functions.R")
604+
options(error = recover)
605+
g("a")
606+
options(error = NULL)
607+
```
608+
609+
<br>
610+
611+
![](img/options-error-recover.PNG)
612+
561613
## trace() overview
562614
563615
`trace()` is a more flexible version of `debug()`
564616
565-
* `trace(fun, browser)` = `debug(fun)`
617+
- `trace(fun, browser)` = `debug(fun)`
566618
567-
* equivalent to inserting `broswer()` in first line of function
619+
- equivalent to inserting `broswer()` in first line of function
568620
569621
. . .
570622
571-
you an also insert _any_ code at _any_ location in function
623+
you an also insert *any* code at *any* location in function
572624
573-
* `trace(fun, browser, at = 2)` <br>inserts `browser()` at second step of `fun`
625+
- `trace(fun, browser, at = 2)` <br>inserts `browser()` at second step of `fun`
574626
575627
. . .
576628
577-
578629
`untrace(fun)` cancels the tracing
579630
580631
⚠️ `trace()` requires `{` in function definition
@@ -592,20 +643,18 @@ untrace(colSums)
592643

593644
<br>
594645

595-
* `trace(colSums, browser)` = `debug(colSums)`
646+
- `trace(colSums, browser)` = `debug(colSums)`
596647

597-
* equivalent to inserting `broswer()` in first line of function <br>
598-
if we had the source code
648+
- equivalent to inserting `broswer()` in first line of function <br> if we had the source code
599649

600650
## navigating function steps
601651

602652
<br>
603653

604-
investigate 🧐 the function with `as.list()` + `body()`
654+
investigate 🧐 the function with `as.list()` + `body()`
605655

606656
<brshort>
607657

608-
609658
```{r, eval=FALSE, echo=TRUE}
610659
x <- as.list(body(colSums))
611660
View(x)
@@ -616,22 +665,16 @@ View(x)
616665
. . .
617666

618667
::: columns
619-
620668
::: {.column width="40%"}
621-
622669
![](img/colSums-body.PNG)
623-
624670
:::
625671

626672
::: {.column width="2%"}
627673
:::
628674

629675
::: {.column width="58%"}
630-
631676
![](img/colSums-list.PNG)
632-
633677
:::
634-
635678
:::
636679

637680
## identify function step
@@ -657,30 +700,85 @@ colSums(1:3)
657700
untrace(colSums)
658701
```
659702
660-
661-
662703
# Special cases
663704
664705
## Warnings
665706
666-
If you want to dig deeper into a warning, you can convert them to errors.
707+
<br>
708+
709+
If you want to dig deeper into a warning, you can convert them to errors to initiate
710+
debugging tools.
711+
712+
<brshort>
713+
714+
```{r, eval=FALSE, echo=TRUE}
715+
?options
716+
options(warn = 0) # default, stores warnings until top-level function returns
717+
options(warn = 1) # warnings are printed as they occur
718+
options(warn = 2) # upgrades warnings to errors
719+
720+
721+
# initiate recover on warning
722+
options(warn = 2, error = recover)
723+
# restore original settings
724+
options(warn = 0, error = NULL)
667725
668-
```{r}
669-
#options(warn = 0) # default, warnings deferred to end
670-
options(warn = 1) # print warning when it happens
671-
options(warn = 2) # converts all warnings to errors, allows you to use regular debugging tools
672726
```
673727

674728
## Piped expressions
675729

676-
## Rmarkdown
730+
<br>
731+
732+
tracebacks can be verbose with pipes
733+
734+
. . .
735+
736+
<br>
737+
738+
```{r, eval=FALSE, echo=TRUE}
739+
options(error = rlang::entrace, rlang_backtrace_on_error = "branch")
740+
```
741+
742+
gives trimmed tracebacks when using pipes
677743

678-
## stuff
744+
. . .
745+
746+
<br>
679747

680-
rmarkdown chunk option `error=TRUE`
748+
* more [contributed answers on RStudio Community](https://community.rstudio.com/t/whats-currently-the-recommended-way-to-debug-pipe-chains/14724/3)
681749

682-
`knitr::knit_exit()`
750+
* Matt Dray 2019 blog post [Fix leaky pipes in R](https://www.rostrum.blog/2019/04/07/fix-leaky-pipes/)
751+
752+
## Rmarkdown {.smaller}
753+
754+
<brshort>
755+
756+
#### Troubleshooting:
757+
758+
1. rmarkdown chunk option `error = TRUE` enables knitting with errors
759+
760+
2. insert `knitr::knit_exit()` and interactively work through .Rmd
761+
762+
. . .
763+
764+
<br>
765+
766+
#### Debugging:
767+
768+
1. Adv R Ch 22.5.3 RMarkdown <https://adv-r.hadley.nz/debugging.html#rmarkdown>
769+
770+
2. WTF Ch 11.4 Debugging in Rmarkdown documents <https://rstats.wtf/debugging-r-code.html#debugging-in-r-markdown-documents>
771+
772+
##
773+
774+
::: r-fit-text
775+
**Go forth, and learn from your bugs!**
776+
:::
777+
778+
![](img/butterflies.gif){fig-align="center"}
779+
780+
:::footer
781+
<https://giphy.com/gifs/TreehouseDirect-max-and-ruby-maxandruby-maxruby-S3qtrEqiU5OuwAtg5Z>
782+
:::
683783

684-
more
685784

686-
## Shiny

0 commit comments

Comments
 (0)