The Art of R Programming

(WallPaper) #1
NOTE Operation ofedtdbgfor Emacs is the same as shown here, just with different keystrokes
used for the commands. For instance, F8 is used for single-stepping instead of [.

First, note that the editor’s cursor is now on this line:

wmins <- apply(dd[-n, ], 1, imin)

This shows the line to be executed next.
Whenever I want to single-step a line, I simply hit the [ key while I’m in
the editor window. The editor then tells the browser to execute itsncom-
mand, without my needing to move the mouse to the R execution window,
and then the editor moves its cursor to the next line. I can also hit ] for the
browser’sccommand. Each time I execute a line or lines in this manner, the
editor’s cursor moves right along.
Whenever I make a change to my source code, typing,src(the comma
is part of the command) into my GVim window will tell R to callsource()
on it. Each time I want to rerun my code, I hit,dt. I rarely, if ever, need to
move my mouse away from the editor window to the R window and back.
In essence, the editor has become my debugger in addition to providing its
editing operations.

13.5 Ensuring Consistency in Debugging Simulation Code...........................


If you’re doing anything with random numbers, you’ll need to be able to
reproduce the same stream of numbers each time you run your program
during the debugging session. Without this, your bugs may not be repro-
ducible, making them even harder to fix.
Theset.seed()function controls this by reinitializing the random num-
ber sequence to a given value.
Consider this example:

[1] 0.8811480 0.2853269 0.5864738
> runif(3)
[1] 0.5775979 0.4588383 0.8354707
> runif(3)
[1] 0.4155105 0.4852900 0.6591892
> runif(3)
> set.seed(8888)
> runif(3)
[1] 0.5775979 0.4588383 0.8354707
> set.seed(8888)
> runif(3)
[1] 0.5775979 0.4588383 0.8354707

The callrunif(3)generates three random numbers from the uniform
distribution on the interval (0,1). Each time we make that call, we will get a
different set of three numbers. But withset.seed(), we can start over and get
the same sequence of numbers.

302 Chapter 13

Free download pdf