@@ -199,6 +199,14 @@ Troubleshooting strategies:
199
199
200
200
3 . Reprex
201
201
202
+ ## But what if
203
+
204
+ ![ ] ( img/swat-bugs.gif )
205
+
206
+ ::: footer
207
+ < https://giphy.com/gifs/TreehouseDirect-agent-binky-8UabbehIqkR0akDt1y >
208
+ :::
209
+
202
210
# Moving on
203
211
204
212
<br >
@@ -213,6 +221,7 @@ from troubleshooting to...
213
221
formal ` debugging ` techniques.
214
222
:::
215
223
224
+
216
225
## Key concepts
217
226
218
227
<br >
@@ -360,7 +369,7 @@ used interactively in console, called for last error observed
360
369
the trace back is also known as: <br > call stack, stack trace, & back trace
361
370
:::
362
371
363
- ## Richer trace back
372
+ ## Richer traceback
364
373
365
374
<br >
366
375
@@ -373,7 +382,7 @@ rlang::last_error()
373
382
374
383

375
384
376
- ## Richer trace back
385
+ ## Richer traceback
377
386
378
387
<br>
379
388
@@ -391,6 +400,21 @@ rlang::last_trace()
391
400
392
401
` options(error = rlang::entrace) ` <br > could go in your ` .Rprofile `
393
402
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
+
394
418
## browser()
395
419
396
420
```{r, eval=FALSE, echo=TRUE, ` code-line-numbers ` ="2"}
@@ -558,23 +582,50 @@ g("a")
558
582
559
583
- interactive debugger initiated a ** single** time when ` g() ` is executed
560
584
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
+ 
612
+
561
613
## trace() overview
562
614
563
615
`trace()` is a more flexible version of `debug()`
564
616
565
- * ` trace(fun, browser) ` = ` debug(fun) `
617
+ - `trace(fun, browser)` = `debug(fun)`
566
618
567
- * equivalent to inserting ` broswer() ` in first line of function
619
+ - equivalent to inserting `broswer()` in first line of function
568
620
569
621
. . .
570
622
571
- you an also insert _ any _ code at _ any _ location in function
623
+ you an also insert *any* code at *any* location in function
572
624
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`
574
626
575
627
. . .
576
628
577
-
578
629
`untrace(fun)` cancels the tracing
579
630
580
631
⚠️ `trace()` requires `{` in function definition
@@ -592,20 +643,18 @@ untrace(colSums)
592
643
593
644
<br >
594
645
595
- * `trace(colSums, browser)` = `debug(colSums)`
646
+ - ` trace(colSums, browser) ` = ` debug(colSums) `
596
647
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
599
649
600
650
## navigating function steps
601
651
602
652
<br >
603
653
604
- investigate 🧐 the function with `as.list()` + `body()`
654
+ investigate 🧐 the function with ` as.list() ` + ` body() `
605
655
606
656
<brshort >
607
657
608
-
609
658
``` {r, eval=FALSE, echo=TRUE}
610
659
x <- as.list(body(colSums))
611
660
View(x)
@@ -616,22 +665,16 @@ View(x)
616
665
. . .
617
666
618
667
::: columns
619
-
620
668
::: {.column width="40%"}
621
-
622
669
![ ] ( img/colSums-body.PNG )
623
-
624
670
:::
625
671
626
672
::: {.column width="2%"}
627
673
:::
628
674
629
675
::: {.column width="58%"}
630
-
631
676
![ ] ( img/colSums-list.PNG )
632
-
633
677
:::
634
-
635
678
:::
636
679
637
680
## identify function step
@@ -657,30 +700,85 @@ colSums(1:3)
657
700
untrace(colSums)
658
701
```
659
702
660
-
661
-
662
703
# Special cases
663
704
664
705
## Warnings
665
706
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)
667
725
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
672
726
```
673
727
674
728
## Piped expressions
675
729
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
677
743
678
- ## stuff
744
+ . . .
745
+
746
+ <br >
679
747
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 )
681
749
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
+ :::
683
783
684
- more
685
784
686
- ## Shiny
0 commit comments