Debug in R

Development in R usually is performed using JGR, or Tinn-R, editors and the R console.
Since these are no “state of the art” IDEs for R development, this is usually the best one can have.

So, how does one debug a program in R?
The usual answer is classical: with print.
But the correct answer is: using the browser() function.

The browser() function works as a break point marker.
Just write browser() where a break point is needed and run the program.
When browser() is interpreted, the program hang its execution and a new command line prompt will be provided, Browse[1]>.
This command line allows one to interact with the variables available in the scope where the program is temporary hanged.
This debug command line accepts the following debug commands:

  • n: next, advances to the next line, allows step-by-step.
  • c: continue, continues the execution, until the program ends or a new browser() function is found.
  • traceback(), shows the stack trace function call.
  • Q: quit, stop the program execution.

It does not look as cool as using the mouse to insert a break point on a line, but it works.