Skip to content

Instantly share code, notes, and snippets.

@peterk87
Created April 26, 2013 20:42
Show Gist options
  • Save peterk87/5470305 to your computer and use it in GitHub Desktop.
Save peterk87/5470305 to your computer and use it in GitHub Desktop.
R: track down errors that come primarily from non-interactive R sessions; prints a stack trace and continues on
# Printing stack trace and continuing after error occurs in R
# http://stackoverflow.com/questions/1975110/printing-stack-trace-and-continuing-after-error-occurs-in-r
# Bob Albright
# 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()
}
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment