-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintro.R
92 lines (69 loc) · 1.77 KB
/
intro.R
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
## Debugging in R
# let's make a dumb function
broken <- function(len) {
for(i in 1:len) {
x <- i + runif(1)
}
return(x)
}
broken(10)
# Hmmmmm
# That didn't work how we wanted
# we can enter in to a given execution context with
# browser()
broken <- function(len) {
for(i in 1:len) {
x <- i + runif(1)
browser()
}
return(x)
}
# we see and get access to the REPL
# Called from: broken(10)
# Browse[1]>
# From here we can work just as we would in the console normally
# ls() returns files in the workspace while in the console,
# but it is context specific
# (we can even specify the context if we want)
# typing ls()...
# [1] "i" "len" "x"
# Cool! What do we get?
## The object we created implicitly: "i"
## The object we passed in as an arg: "len"
## The object we created explicitly with `<-`: "x"
# we can advance our browser
# (not quite a debugger, we'll get there!)
# by typing c in the console.
# because we entered in a loop,
# we'll call the browser at each iteration
# That control is a little coarse. We want 2 things:
## The ability to inspect objects
## the ability to step through execution
# For most functions
# (specifically and importantly including your code)
# n in the console will transition to a step through debugger
# we can inspect line by line, not just each browser call
fixed <- function(len) {
x <- numeric()
for(i in 1:len) {
x[i] <- i + runif(1)
}
return(x)
}
# Or...
# fixedBetter <- function(len) {
# return(1:len + runif(len))
# }
# one last bit
# just like anything else, we can put the
# browser in a conditional:
brokenCond <- function(len) {
for(i in 1:len) {
x <- i + runif(1)
if(length(x) < len) {
browser()
}
}
return(x)
}
# This is great, but we're starting to see the problem...