Contents

Use print() statements in your functions to print variable values. Although this technique is considered low-tech or even old fashioned by some, it can still be a quick and easy way to trace an error.

Place a browser() statement in the function just before the crashing line. When the function is called, it will be executed up to the browser() line. The command-line interface then switches to the function environment, so that all variables in the function can be inspected or changed. See below for commands available in browser() mode.

Then, when the function is called, and a browser in that function's environment is opened so that it can be executed line by line. In the debugging browser, apart from all standard R functionality, the following commands are available.

Command

Meaning

n

Advance to next step. An empty line also works.

c, cont

Continue to the end of the current context. E.g. to the end the loop within a loop or to the end of the function.

By typing '1' or '2' behind Selection: the browser will jump to the selected environment. Once in the browser, all standard R functionality is at your disposal, as well as the commands in the table below.

Command

Meaning

c, cont

Exit the browser and continue at the next statement. An empty line will do the same.

n

Enter the step-through debugger (this changes the meaning of c)

where

Print a stack trace of active function calls (where are you in the stack?).

Q

Exit the browser, do not continue at the next statement but go back to the top-level R browser.