5.9 Fixing errors: Tracebacks

  • In R, every error is accompanied by a traceback, or call stack, which literally traces back through the sequence of calls that lead to the error

  • The functions are printed in reverse order

  • The traceback tool pinpoints the location of an error.

5.9.1 Example of reading traceback

f <- function(x) g(x)
g <- function(x) h(x)
h <- function(x) 2 * 2

f(3)
## [1] 4
f <- function(x) g(x)
g <- function(x) h(x)
h <- function(x) x * 2

f("a")
  • This will generate an error below
f("a")
#> Error in x * 2: non-numeric argument to binary operator
  • The traceback is shown below:
  • Top of the stack points to an error
traceback()

#> 3: h(x)
#> 2: g(x)
#> 1: f("a")
  • Flipping the traceback shows the better sequence(the top of the stack points to an error location)

  • shows sequence of calls that lead to the error — f() called g() called h() (which errors)

1: f("a")
2: g(x)
3: h(x)