traceback() for interactive and non-interactive R sessions
With default values of its arguments, traceback()
will look for an object named .Traceback
in the baseenv()
for information on the call stack. It looks (from src/main/errors.c
) like .Traceback
is only created if, among other conditions, R_Interactive || haveHandler
, suggesting that this object is not created during non-interactive sessions. If there is no object named .Traceback
, you will get the message "No traceback available".
However, by passing a non-NULL value to the x
argument of traceback()
, one can obtain information about the call stack from a non-interactive session. With a non-zero integer value (indicating the number of calls to skip in the stack), c-level functions (R_GetTraceback
) are called to investigate the call stack instead of looking in .Traceback
.
So there are a couple ways to obtain traceback information in a non-interactive session:
f = function() {
on.exit(traceback(1))
1 + 'a'
}
f()
Or, setting options
as Brandon Bertelsen suggested
options(error=function()traceback(2))
The different values passed to x
in the two examples account for the different number of functions to skip
In the
on.exit
example,traceback(1)
skips the call totraceback()
.In the example setting
options
, there is an extra anonymous function that callstraceback()
which should/could also be skipped.
In the example in the OP, there's not much more information gained by using traceback()
compared to the automatic traceback provided in the case of an error in a non-interactive session. However, with functions that take (and are passed) arguments, using traceback()
will be much more informative than the standard presentation of the call stack in the non-interactive session.
Always show traceback() in interactive R
Use
options(error = traceback)
and you'll automatically get a traceback when an error occurs. An even more useful choice is
options(error = utils::recover)
which prints something like a traceback but lets you examine the variables as they existed at the time of the error, or
options(error = utils::dump.frames)
which saves the variables so they can be examined later.
Any one of these commands could be put in your .Rprofile
file and they would automatically apply in any session that used it.
Non-interactive tar_visnetwork()/tar_glimpse()?
tar_visnetwork
creates a HTML widget. You can render this file in your local Browser:
library(targets)
library(htmltools)
save_html(html = tar_visnetwork(), file = "dag.html")
How to get R script line numbers at error?
This won't give you the line number, but it will tell you where the failure happens in the call stack which is very helpful:
traceback()
[Edit:] When running a script from the command line you will have to skip one or two calls, see traceback() for interactive and non-interactive R sessions
I'm not aware of another way to do this without the usual debugging suspects:
- debug()
- browser()
- options(error=recover) [followed by options(error = NULL) to revert it]
You might want to look at this related post.
[Edit:] Sorry...just saw that you're running this from the command line. In that case I would suggest working with the options(error) functionality. Here's a simple example:
options(error = quote({dump.frames(to.file=TRUE); q()}))
You can create as elaborate a script as you want on an error condition, so you should just decide what information you need for debugging.
Otherwise, if there are specific areas you're concerned about (e.g. connecting to a database), then wrap them in a tryCatch() function.
Related Topics
Duplicate a Column in Data Frame and Rename It to Another Column Name
Real Time, Auto Updating, Incremental Plot in R
Convert Data from Many Rows to Many Columns
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Really Fast Word Ngram Vectorization in R
Dplyr 'Rename' Standard Evaluation Function Not Working as Expected
Creating Tree Diagram for Showing Case Count Using R
R: Unexpected Results from P.Adjust (Fdr)
Raster Package Taking All Hard Drive
Using Dplyr for Frequency Counts of Interactions, Must Include Zero Counts
Is There Any Other Package Other Than "Sentiment" to Do Sentiment Analysis in R
How to Remove the Legend Title in Ggplot2
Print Number as Reduced Fraction in R
Passing Arguments to Iterated Function Through Apply