-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiCode_debug.qmd
More file actions
136 lines (86 loc) · 2.71 KB
/
iCode_debug.qmd
File metadata and controls
136 lines (86 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
---
title: "Debugging"
title-slide-attributes:
data-background-image: fig/iCode_logo_simple.png
data-background-size: contain
data-background-opacity: "0.2"
format:
revealjs:
theme: default
editor: visual
author: Guillaume Patoine
logo: fig/iCode_logo_simple.png
footer: "iCode Seminar Series"
toc: true
toc-title: Contents
preview-links: true
---
## When it breaks
{height=450}
{height=450}
# 1.Manual debugging
## Finding the error(s)
When you encounter an error
::: {.alert .alert-info style="text-align: center; max-width: 800; margin: auto; border: 3px solid black; padding: 10px;"}
Finding your bug is a process of confirming the many things that you believe are true — until you find one which is not true.
—Norm Matloff
:::
## Accessing source code
- F1 for documentation
- print (no comment)
- F2 (no comment)
- check source code (Github/CRAN)
## View traceback
{height=300 fig-align="center"}
- traceback()
- rlang::last_trace()
## View traceback {.unnumbered .unlisted}
```{r, echo=TRUE, error=TRUE}
f <- function(a) g(a)
g <- function(b) h(b)
h <- function(c) i(c)
i <- function(d) {
if (!is.numeric(d)) {
stop("`d` must be numeric", call. = FALSE)
}
d + 10
}
f("a")
```
```
> traceback()
5: stop("`d` must be numeric", call. = FALSE) at #3
4: i(c) at #1
3: h(b) at #1
2: g(a) at #1
1: f("a")
```
## Tips and tricks
- Can you reproduce it?
- Simplify the problem, make a reprex
**Run the function line by line in the global environment**
- Input argument values
- Add `print()` calls
- Check expected results along way
# 2. Advanced tools to dive/dig into the function
## Interactive debugger interface (browser mode)
{fig-align="center"}
## Exercise 1: How to use debugger
- interactive "Rerun with debug"
- `browser()`: in code, when run
- breakpoints: in IDE, when line reached
- recover: set in options, starts at time of error
- `debug()`: run with function name as arguments
Exercise 1: (https://github.com/gpatoine/iCode_debug/sample_script.R)
## Exercise 2: Error of your choice
Pick a function to look into, or try to resolve an error that you had.
Examples: `sample()`, `lm()`, `ggplot`, `gptools::coldesc()`
Look into how the functions are built, you can relate to the function writing guidelines from the function iCode series. Are checks performed.
Break the functions and see how errors are handled?
# Sources and references
- Advanced R book: Debugging chapter <br>
<https://adv-r.hadley.nz/debugging.html>
- Jenny Brian talk <br>
<https://www.youtube.com/watch?v=vgYS-F8opgE)>
{height="280"}
{height="280"}