Jeroen Jeroen - 11 months ago 62
R Question

Save traceback on error using tryCatch

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

in an interactive session. However,
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")

errhandler <- function(e){
stacktrace <- traceback()

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
manually after the error:


Answer Source

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.