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()