The Art of R Programming

(WallPaper) #1
The browser will be invoked only ifsis larger than 1. The following would
have the same effect:

if (s > 1) browser()

Calling the browser directly, rather than entering the debugger via
debug()is very useful in situations in which you have a loop with many iter-
ations and the bug surfaces only after, say, the 50th iteration. If the loop
index isi, then you could write this:

if (i > 49) browser()

That way, you would avoid the tedium of stepping through the first 49
iterations!

13.3.3.2 Using the setBreakpoint() Function
Starting with R 2.10, you can usesetBreakpoint()in the format

setBreakpoint(filename,linenumber)

This will result inbrowser()being called at linelinenumberof our source file
filename.
This is especially useful when you are in the midst of using the debug-
ger, single-stepping through code. Say you are currently at line 12 of your
source filex.Rand want to have a breakpoint at line 28. Instead of exiting
the debugger, adding a call tobrowser()at line 28, and then re-entering the
function, you could simply type this:

> setBreakpoint("x.R",28)

You could then resume execution within the debugger, say by issuing
theccommand.
ThesetBreakpoint()function works by calling thetrace()function, dis-
cussed in the next section. Thus, to cancel the breakpoint, you cancel the
trace. For instance, if we had calledsetBreakpoint()at a line in the function
g(), we would cancel the breakpoint by typing the following:

> untrace(g)

You can callsetBreakpoint()whether or not you are currently in the
debugger. If you are not currently running the debugger and you execute
the affected function and hit the breakpoint during that execution, you will
be put into the browser automatically. This is similar to the case ofbrowser(),
but using this approach, you save yourself the trouble of changing your code
via your text editor.

290 Chapter 13

Free download pdf