When calling an R function inside my webapp, I would like to catch the stack-trace when an error occurs and present it to the user for debugging purposes. Something like the output of traceback()
in an interactive session. However, traceback
doesn't seem to work when it is called inside the error handler, it returns No traceback available
:
f <- function() {
g <- function() stop("test traceback")
g()
}
errhandler <- function(e){
stacktrace <- traceback()
unlist(stacktrace);
}
out <- tryCatch(f(), error=errhandler)
Is there any way I can programatically catch the stack trace of an error? I.e. get the output that I would get when calling traceback()
manually after the error:
f()
traceback()
It turns out that the latest version of the evaluate
package has a function called try_capture_stack
which is a pretty good implementation of this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With