n <- length(x)
runs <- NULL
for (i in 1:(n - k)) {
if (all(x[i:i+k-1]==1))
runs <- c(runs, i)
}
return(runs)
}
attr(,"srcfile")
findruns.R
So, according to the principle of confirmation, let’s first make sure our
test vector was received correctly:
Browse[2]> x
[1]100110111
So far, so good. Let’s step through the code a bit. We hit n a couple of
times to single-step through the code.
Browse[2]> n
debug at findruns.R#2: n <- length(x)
Browse[2]> n
debug at findruns.R#3: runs <- NULL
Browse[2]> print(n)
[1] 9
Note that after each single step, R tells us which statement would be the
nextone to execute. In other words, at the time we executedprint(n),wehad
notyet executed the assignment ofNULLtoruns.
Note, too, that although normally you can print out the value of a vari-
able by simply typing its name, we could not do so here for our variablen,
becausenis also the abbreviation for the debugger’s next command. Thus,
we neededprint().
At any rate, we found that the length of our test vector was 9, confirming
what we knew. Now, let’s single-step some more, getting into the loop.
Browse[2]> n
debug at findruns.R#4: for (i in 1:(n-k+1)){
if (all(x[i:i+k-1]==1))
runs <- c(runs, i)
}
Browse[2]> n
debug at findruns.R#4: i
Browse[2]> n
debug at findruns.R#5: if (all(x[i:i+k-1]==1))runs <- c(runs, i)
Debugging 293