29  R: traceback

29.1 Console

  • It is an error handler that is called immediately after encountering an error

  • You can set your .Rprofile or set options as: options(error = traceback)

  • To remove the options: options(error = NULL)

  • Check ?traceback for further details

  • To use traceback, run following function call that throws error

  • In R console, type: traceback()

  • The default display is of the stack of the last uncaught error

  • The stack of calls always contains all function calls and all foreign function calls in the call sequence with line number

29.1.1 traceback: Call 1

This call does not display an error; so traceback is not available

Code
X = c(NA, 12, -15, 14, 18)
fn_Mean(X, na.rm = TRUE)
$n
[1] 4

$AM
[1] 7.25

$GM
[1] NaN

$HM
[1] 27.8453
Code
No traceback available 

29.1.2 traceback: Call 2

This function call displays an error; so traceback is available

Code
X = c(NA, 12, 0, "A", 18)
fn_Mean(X, na.rm = TRUE)
traceback()

29.2 RStudio Environment

  • Select: Debug > On Error > Error Inspector

  • Run the following function and explore traceback

29.2.1 traceback: Call 3

This function call displays an error; so traceback is available

Code
X = c(NA, 12, 0, "A", 18)
fn_Mean(X, na.rm = TRUE)

29.2.2 RStudio menu

29.2.3 traceback indicator

29.2.4 traceback information