forked from rdpeng/CourseraLectures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebugging.tex
253 lines (216 loc) · 6.3 KB
/
debugging.tex
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
\documentclass[aspectratio=169]{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
% or ...
\setbeamercovered{transparent}
% or whatever (possibly just delete it)
}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath,amsfonts,amssymb}
\input{macros}
\title[Debugging]{Debugging}
\date{Computing for Data Analysis}
\setbeamertemplate{footline}[page number]
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}{Something's Wrong!}
Indications that something's not right
\begin{itemize}
\item \code{message}: A generic notification/diagnostic message
produced by the \code{message} function; execution of the function
continues
\item \code{warning}: An indication that something is wrong but not
necessarily fatal; execution of the function continues; generated by
the \code{warning} function
\item \code{error}: An indication that a fatal problem has occurred;
execution stops; produced by the \code{stop} function
\item \code{condition}: A generic concept for indicating that
something unexpected can occur; programmers can create their own
conditions
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Something's Wrong!}
Warning
\begin{verbatim}
> log(-1)
[1] NaN
Warning message:
In log(-1) : NaNs produced
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Something's Wrong}
\begin{verbatim}
printmessage <- function(x) {
if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Something's Wrong}
\begin{verbatim}
printmessage <- function(x) {
if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
> printmessage(1)
[1] "x is greater than zero"
> printmessage(NA)
Error in if (x > 0) { : missing value where TRUE/FALSE needed
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Something's Wrong!}
\begin{verbatim}
printmessage2 <- function(x) {
if(is.na(x))
print("x is a missing value!")
else if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{Something's Wrong!}
\begin{verbatim}
printmessage2 <- function(x) {
if(is.na(x))
print("x is a missing value!")
else if(x > 0)
print("x is greater than zero")
else
print("x is less than or equal to zero")
invisible(x)
}
> x <- log(-1)
Warning message:
In log(-1) : NaNs produced
> printmessage2(x)
[1] "x is a missing value!"
\end{verbatim}
\end{frame}
\begin{frame}{Something's Wrong!}
How do you know that something is wrong with your function?
\begin{itemize}
\item What was your input? How did you call the function?
\item What were you expecting? Output, messages, other results?
\item What did you get?
\item How does what you get differ from what you were expecting?
\item Were your expectations correct in the first place?
\item Can you reproduce the problem (exactly)?
\end{itemize}
\end{frame}
\begin{frame}{Debugging Tools in R}
The primary tools for debugging functions in R are
\begin{itemize}
\item \code{traceback}: prints out the function call stack after an
error occurs; does nothing if there's no error
\item \code{debug}: flags a function for ``debug'' mode which allows
you to step through execution of a function one line at a time
\item \code{browser}: suspends the execution of a function wherever it
is called and puts the function in debug mode
\item \code{trace}: allows you to insert debugging code into a
function a specific places
\item \code{recover}: allows you to modify the error behavior so that
you can browse the function call stack
\end{itemize}
These are interactive tools specifically designed to allow you to pick
through a function. There's also the more blunt technique of inserting
\code{print}/\code{cat} statements in the function.
\end{frame}
\begin{frame}[fragile]{traceback}
\begin{verbatim}
> mean(x)
Error in mean(x) : object 'x' not found
> traceback()
1: mean(x)
>
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{traceback}
\begin{verbatim}
> lm(y ~ x)
Error in eval(expr, envir, enclos) : object 'y' not found
> traceback()
7: eval(expr, envir, enclos)
6: eval(predvars, data, env)
5: model.frame.default(formula = y ~ x, drop.unused.levels = TRUE)
4: model.frame(formula = y ~ x, drop.unused.levels = TRUE)
3: eval(expr, envir, enclos)
2: eval(mf, parent.frame())
1: lm(y ~ x)
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{debug}
\begin{verbatim}
> debug(lm)
> lm(y ~ x)
debugging in: lm(y ~ x)
debug: {
ret.x <- x
ret.y <- y
cl <- match.call()
...
if (!qr)
z$qr <- NULL
z
}
Browse[2]>
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{debug}
\begin{verbatim}
Browse[2]> n
debug: ret.x <- x
Browse[2]> n
debug: ret.y <- y
Browse[2]> n
debug: cl <- match.call()
Browse[2]> n
debug: mf <- match.call(expand.dots = FALSE)
Browse[2]> n
debug: m <- match(c("formula", "data", "subset", "weights", "na.action",
"offset"), names(mf), 0L)
\end{verbatim}
\end{frame}
\begin{frame}[fragile]{recover}
\begin{verbatim}
> options(error = recover)
> read.csv("nosuchfile")
Error in file(file, "rt") : cannot open the connection
In addition: Warning message:
In file(file, "rt") :
cannot open file 'nosuchfile': No such file or directory
Enter a frame number, or 0 to exit
1: read.csv("nosuchfile")
2: read.table(file = file, header = header, sep = sep, quote = quote, dec = de
3: file(file, "rt")
Selection:
\end{verbatim}
\end{frame}
\begin{frame}{Debugging}
Summary
\begin{itemize}
\item There are three main indications of a problem/condition:
message, warning, error; only an error is fatal
\item When analyzing a function with a problem, make sure you can
reproduce the problem, clearly state your expectations and how the
output differs from your expectation
\item Interactive debugging tools \code{traceback}, \code{debug},
\code{browser}, \code{trace}, and \code{recover} can be used to
find problematic code in functions
\item Debugging tools are not a substitute for thinking!
\end{itemize}
\end{frame}
\end{document}