I'm writing some R code that calls other code that may fail. If it does, I want to print a stack trace (to track down what went wrong), then carry on regardless. However, the traceback() function only provides information about uncaught exceptions. I can get the result I want via a rather complex, natty construction involving tryCatch and dump.frames, but is there not an easier way of doing this?
A:
I think that you will need to use tryCatch()
. You can do whatever you want in the tryCatch() function, so it's not clear to me why you are viewing this as complex. Maybe post your code example?
Shane
2009-12-29 15:14:13
Complex as compared to most other languages I use, e.g. Java or Python, in which printing a stack trace from an exception is a no-brain one-liner.
chrispy
2009-12-29 15:32:53
I still don't see why what you're describing would be much more than a one-liner. The only difficulty is if you're trying to throw a specific exception type, because that isn't readily supported.
Shane
2009-12-29 15:37:15
Perhaps it's not -- if so, please post how you would do it! :)
chrispy
2009-12-29 16:36:20
+1
A:
Have you tried the
options(error=recover)
setting? Chambers 'Software for Data Analysis' has some useful hints on debugging.
Dirk Eddelbuettel
2009-12-29 15:14:25
I don't want an interactive prompt, I want the program to print out the stack trace and carry on regardless.
chrispy
2009-12-29 16:29:05
Are you working with R code only or also with other languages you glue to R?
Dirk Eddelbuettel
2009-12-29 17:26:54
+2
A:
I wrote this code about a week ago to help me track down errors that come primarily from non-interactive R sessions. It's still a little rough, but it prints a stack trace and continues on. Let me know if this is useful, I'd be interested in how you would make this more informative. I'm also open into cleaner ways to get this information.
options(warn = 2, keep.source = TRUE, error =
quote({
cat("Environment:\n", file=stderr());
# TODO: setup option for dumping to a file (?)
# Set `to.file` argument to write this to a file for post-mortem debugging
dump.frames(); # writes to last.dump
#
# Debugging in R
# http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/index.shtml
#
# Post-mortem debugging
# http://www.stats.uwo.ca/faculty/murdoch/software/debuggingR/pmd.shtml
#
# Relation functions:
# dump.frames
# recover
# >>limitedLabels (formatting of the dump with source/line numbers)
# sys.frame (and associated)
# traceback
# geterrmessage
#
# Output based on the debugger function definition.
n <- length(last.dump)
calls <- names(last.dump)
cat(paste(" ", 1L:n, ": ", calls, sep = ""), sep = "\n", file=stderr())
cat("\n", file=stderr())
if (!interactive()) {
q()
}
}))
PS: you might not want warn=2 (warnings converted to errors)
Bob Albright
2010-01-04 17:00:55
I like this. To make it more informative, you could call `ls.str()` for each environmnent in `last.dump`. (This could make the output quite lengthy though.)
Richie Cotton
2010-01-06 11:28:54
Not quite what I was after, but it does at least address printing a stack trace. Thanks!
chrispy
2010-01-08 10:12:27