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
traceis a more flexible version ofdebugfunction.A call to
traceallows 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
?tracefor further details.trace(what = fun, tracer = browser)is equivalent to insertingbrowser()in the first line of functiontrace(what = fun, tracer = browser, at = 2)insertbrowser()at the second lineYou need to
untrace(function_name)after exploring the function.
33.2
recover
The function
recoverallows the user to browse directly on any of the currently active function calls, and is suitable as an error optionThe 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
?recoverfor further details.