-
Notifications
You must be signed in to change notification settings - Fork 2
/
shiny-debugging.Rmd
298 lines (195 loc) · 6.28 KB
/
shiny-debugging.Rmd
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
author: "Jonathan McPherson"
title: Debugging with Shiny
output:
ioslides_presentation:
css: shiny-slides.css
logo: images/shiny-logo.png
self_contained: no
incremental: true
---
## About me
![stanford baby](images/stanford-baby.jpg)
<div class="notes">
Here's me about 35 years ago, at the Stanford Medical Center, just a few blocks away.
</div>
## About me
- Software engineer since mid-90's
- RStudio IDE developer since 2013
- Worked on RStudio debugging features
<div class="notes">
Web developer, worked at Microsoft on Office.
</div>
## Goals
### You will learn:
<div class="columns-2">
- Tools for finding bugs & diagnosing errors in Shiny applications
- Little-known ways to look under the hood
- How to debug problems in deployed apps
- Cheat code for OSX IDE JavaScript debug tools
![toolbelt](images/toolbelt.jpg)
</div>
<div class="notes">
This is a tools talk. If you're like me, when you debug an app you observe a problem and then you open up your bag of tools and pick one to use to attack the problem. Our goal here today is to fill your bag with lots of tools so you have the right one. We'll discuss how to use each tool and when you'd want to.
We'll cover simple and advanced tools. Whether you're new
</div>
## Non-goals
![Advanced R](images/adv-r.jpg)
## Outline
### We'll cover three main areas:
- Debugging
- Tracing
- Errors
# Debugging
"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
Author of *The Art of R Programming* and *The Art of Debugging*
## Breakpoints
![breakpoints](images/breakpoint.png)
*Breakpoint (armed)*
## Breakpoints {.build}
![breakpoints](images/breakpoint-hit.png)
![neo](images/neo-bullets.jpg)
*Stepping after execution*
## Breakpoints
### Pros
- Can inspect local values/stack
- Can step through following code
- Doesn't change code
### Cons
- Only works inside `shinyServer`
- Can't tell you why something isn't happening
- Interrupts program flow; not conditional
## browser()
![browser](images/browser.png)
## Conditional breakpoint
```{r eval=FALSE}
if (input$bins > 50)
browser()
```
## browser()
### Pros
- Similar advantages to breakpoints
- Valid anywhere an R expression is valid
- Can be made conditional
### Cons
- Changes your code!
- Interrupts program flow
- Can't tell you why something isn't happening
# Tracing
## Showcase Mode {.build}
```{r eval=FALSE}
runApp(..., display.mode="showcase")
```
![showcase mode](images/kmeans-showcase.gif)
## Showcase Mode
### Pros
- Very easy to use
- Helps find unnecessary or unexpected execution
### Cons
- Doesn't scale well to larger apps
- Won't show you *why* a reactive or observer executed
## Reactive Log {.build}
```{r eval=FALSE}
options(shiny.reactlog=TRUE)
runApp(...)
showReactLog()
```
![reactive log](images/reactlog.png)
## Reactive Log
### Pros
- *Can* show you why something isn't happening!
- No more thorough way of visualizing reactivity
### Cons
- Lots of data to sift through
- Not real-time
## "printf" tracing {.build}
```{r eval = FALSE}
# generate bins based on input$bins from ui.R
x <- faithful[, 2]
bins <- seq(min(x), max(x), length.out = input$bins + 1)
cat(file=stderr(), "drawing histogram with", input$bins, "bins\n")
```
Listening on http://127.0.0.1:5757
drawing histogram with 30 bins
drawing histogram with 35 bins
<div class="notes">
`file=stderr()` because some parts of Shiny use `capture.output`
</div>
## "printf" tracing
### Pros
- Actual values at runtime, w/o modifying flow
- Only method that works after deployment
### Cons
- Limited to textual representation of values
- Have to guess values to log
- Watch out for reactive side effects!
## "printf" tracing
![heisenbug](images/heisenbug.jpg)
## Tracing on Shinyapps.io {.build}
```{r eval = FALSE}
rsconnect::deployApp( ... )
rsconnect::showLogs(streaming = TRUE)
```
2016-01-29T01:26:12 shinyapps[77594]:
2016-01-29T01:26:12 shinyapps[77594]: Starting R with process ID: '26'
2016-01-29T01:26:14 shinyapps[77594]: drawing histogram with 30 bins
2016-01-29T01:26:14 shinyapps[77594]: drawing histogram with 35 bins
## Tracing in Shiny Server
tail -f /var/log/shiny-server/myapp-20160131-104403-8492.log
Only while R session is alive!
## Client/server tracing {.build}
```{r eval = FALSE}
options(shiny.trace = TRUE)
```
SEND {"config":{"workerId":"","sessionId":"04531d50d12554bd981b24b9d3983cc4"}}
RECV {"method":"init","data":{"bins":30,".clientdata_output_distPlot_width":610,
".clientdata_output_distPlot_height":400,".clientdata_output_distPlot_hidden":false,
".clientdata_pixelratio":1,".clientdata_url_protocol":"http:",
".clientdata_url_hostname":"127.0.0.1",".clientdata_url_port":"5569",
".clientdata_url_pathname":"/",".clientdata_url_search":"",
".clientdata_url_hash_initial":"",".clientdata_singletons":"",
".clientdata_allowDataUriScheme":true}}
# Error Handling
## Tracebacks {.build}
```{r eval=FALSE}
bins <- seq(min(x), max(x), length.out = input$bins + 1)
if (input$bins > 40)
stop("too many bins")
```
Warning: Error in renderPlot: too many bins
Stack trace (innermost first):
76: renderPlot [server.R#20]
68: output$distPlot
1: shiny::runApp
## Full tracebacks {.build}
```{r eval=FALSE}
options(shiny.fullstacktrace = TRUE)
```
Warning: Error in renderPlot: too many bins
Stack trace (innermost first):
79: h
78: .handleSimpleError
77: stop
76: renderPlot [server.R#20]
75: ..stacktraceon..
74: func
...
## Pausing on errors {.build}
```{r eval = FALSE}
options(shiny.error = browser)
```
![Shiny error](images/shiny-error.png)
## JavaScript errors: Browser {.build}
Right-click, inspect element, Console.
![Dev tools](images/chrome-devtools.png)
## JavaScript exceptions: first-chance
![pause on exceptions](images/pauseoncaught.png)
## JavaScript dev mode on OS X {.build}
defaults write org.rstudio.RStudio WebKitDeveloperExtras -bool true
![inspect element](images/inspectelement.png)
## Links
http://shiny.rstudio.com/articles/debugging.html
http://www.github.com/jmcphers/shiny-debug-talk
# Wrapup
Any questions?