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?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
Have you tried the
setting? Chambers 'Software for Data Analysis' has some useful hints on debugging.
I wrote a solution that works like
try
, except that it also returns the call stack.More info in my answer here: https://stackoverflow.com/a/40899766/1587132
no line numbers but this is the closest I found so far:
This is a followup to @chrispy's answer above where he presented a
withJavaLogging
function. I commented that his solution is inspirational, but for me, is marred by some output at the start of the stack trace that I do not want to see.To illustrate, consider this code:
If I execute the line
withJavaLogging( f1() )
I get the outputI do not want to see that
at .handleSimpleError(function (obj)
line followed by the source code of the logger function defined inside thewithJavaLogging
function. I commented above that I could suppress that undesired output by changingtrace = trace[length(trace):1]
totrace = trace[(length(trace) - 1):1]
For the convenience of anyone else reading this, here is a complete version of the function that I now use (renamed from withJavaLogging to logFully, and slightly reformatted to fit my readability preferences):
If I execute the line
logFully( f1() )
I get the output I desire, which is simplyI 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.
PS: you might not want warn=2 (warnings converted to errors)
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?