Debugging code in R

R provides several built in features for stepping through code for analyzing and debugging purposes. As an example you can examine variables during the execution of a script. Amongst others you can use the function debug():

Visual debugging with StatET (Eclipse) & RStudio

Integrated development environments for R offer a visual debugger mode which provides additional debugging features like breakpoints and enhanced visualization. A typical IDE with debugging features is StatET and recently with its preview release v.098 also RStudio 

 Breakpoints

Additional to the approach outlined above, setting breakpoints is a typical feature for debugging. Both IDE’s support breakpoints. If a program run reaches a breakpoint the IDE switches in Debug Mode.

A breakpoint is set by clicking on the script editor’s line number inside or as top-level breakpoints outside of a function. Top-level breakpoints will be activated when the script-file is sourced.

To enable the breakpoints and the functionality of the visual debugger, it is required to source the code. Furthermore StatET has to be started in the debug mode.

Debugger

Debug modus which provides comparably functionality in both IDE’s will automatically be entered when a breakpoint or corresponding R function is reached.

Console (1)

In RStudio the console window shows an additional toolbar, which provides three buttons to navigate through the code, corresponding to the R commands n, c and Q one would also use in StatET.

Source-Script (2)

The expression that is about to be evaluated is highlighted in the source code.

Environment (3)

Corresponding to the displayed .GlobalEnv when debug mode is inactive, the environment pane respectively variables pane in StatET, shows the Objects and values of the local environment, in this case bar().

Traceback (4)

The panel traceback, visible in debug mode, displays the list of current running functions in order of their procedure call, according to the command where in the default browser mode. Moreover by clicking any function in the stack, the functions actual local environment is displayed.

 

Conditional Breakpoints

StatET provides an additional Breakpoints view, to edit and set properties of the breakpoints. Property changes that have been done in this pane takes effect immediately even if the code is already running.

One may also set conditions which determine if a breakpoint is active or enabled. The condition can be any arbitrary R expression which evaluates to TRUE or FALSE.

If you want to use conditional breakpoints in RStudio you have to use the traditional R debugging functions and wrap them in an conditional statement like if(expr) browser() to activate the debugger only in a certain situation.

Conclusion

The debugging features of R are useful options to analyze and debug R scripts. Using a visual debugger makes it much easier. Especially the possibility to highlight the currently executed expression and to view the associated workspaces help to identify errors and to understand the code much better and also look for potential to optimize the code.

With the latest version, RStudio also integrated a visual debugger into its development environment, which is already in the preview version a nearly equivalent tool to the long proven Eclipse / StatET debugger.

The Data Science Training offers the course „Programming with R“ which deals with the subject debugging. For further information:

This way.