27  R Debugging: General

27.1 Debugging tool

Function Explanation
traceback() prints out the function call stack after an error occurs; does nothing if there is no error
debug() flags a function for debug mode which allows to step through execution of a function one line at a time
undebug() to take off the debug mode flags from a function
debugonce() flags a function for debug mode only once; the function does not need undebug after it runs once.
browser() suspends the execution of a function wherever it is called and puts the function in debug mode
trace() allows you to insert debugging code into a function a specific places
recover() allows to modify the error behavior so that you can browse the function call stack


In the Debug mode of browser window, you can move around the function:

  • Type n to execute the next line of code (RStudio: Press Enter or F10 or the relevant debug toolbar icon)

  • Type s to step into the current function call (RStudio: Press Shift+F4 or the relevant debug toolbar icon)

  • Type f to finish remaining of the current function or loop (RStudio: Press Shift+F7 or the relevant debug toolbar icon)

  • Type c to continue the execution until the next breakpoint is encountered (RStudio: Press Shift+F5 or the relevant debug toolbar icon)

  • Type Q to stop and exit the debug mode (RStudio: Press Shift+F8 or the relevant debug toolbar icon)

  • If you have a variable names as n, s, f, c, Q, then use print(variable_name)

  • You can also use other standard R commands to explore the objects in the function environment.

27.2 RStudio Environment