33  R: trace & recover

The functions trace and recover are more flexible version of debugging R function in the console environment although you can still use it in the RStudio environment.

33.1 trace

  • The function trace is a more flexible version of debug function.

  • A call to trace allows you to insert debugging code.

  • The function allows you to start a debugger at the start of a function, or at any location in a function.

  • When a function flagged for debugging is entered, normal execution is suspended and the body of function is executed one statement at a time.

  • Check ?trace for further details.

  • trace(what = fun, tracer = browser) is equivalent to inserting browser() in the first line of function

  • trace(what = fun, tracer = browser, at = 2) insert browser() at the second line

  • You need to untrace(function_name) after exploring the function.

33.2 recover

  • The function recover allows the user to browse directly on any of the currently active function calls, and is suitable as an error option

  • The expression options(error = recover) will make this the error option.

  • Once function exploration is completed, it is convenient to reset it as: options(error = NULL)

  • Check ?recover for further details.