Jon Claus
Jon Claus

Reputation: 2932

Getting traceback in custom error handler in R

I have a function Stop loaded from library lib. I then replace the default error handler in R with this function using options(error = Stop). Within this function, I would like to get the traceback so that I may display the calls leading up to the error being thrown. However, sys.calls does not seem to work within a custom error handler. It only returns a single element, which is is the body of Stop rather than a call. This may have to do with how R intercepts the error handler with Stop. Furthermore, traceback also does not work within Stop. Is there some way I can get a proper traceback in a custom error handler?

Upvotes: 0

Views: 172

Answers (1)

BrodieG
BrodieG

Reputation: 52637

This works for me:

> fun <- function(x) stop('hello')
> Stop <- function() print(sys.calls())
> options(error=Stop)
> fun(1)
Error in fun(1) : hello
[[1]]
fun(1)

[[2]]
stop("hello")

[[3]]
(function () 
print(sys.calls()))()

This is on R 3.1.2. What are you doing different?

Upvotes: 1

Related Questions