Search code examples
rerror-handlingtraceback

Disable storing tracebacks on error


Is there a way to disable the storing of tracebacks on error in R temporarily (for a session)?

The reason I ask is that ggplot2 has a long-running problem, that they've been unable to fix. Somehow the entire dataset gets stored in the traceback, and if you work with very large datasets, this means that a mis-typed variable name can leave you with a 10-minute hang.

Especially when I make complex plots for very large data, this is crippling. Usually these are all small typos, I don't ever need tracebacks, just the error message would be fine.

I tried

options(error = expression(NULL))

but apparently that handler is called after the traceback is stored (the hang persists).

reproducible example

library(ggplot2)
data(diamonds)
diamonds = diamonds[sample(x=nrow(diamonds),size=200000,replace=T),]
qplot(data=diamonds, wrong, var)

Solution

  • One obvious thing that I hadn't thought about is to wrap the call in tryCatch, like this:

    tryCatch({
        print(qplot(data=diamonds, wrong, var))
    }, error = function(e){warning(e)})
    

    It's important to print your plot inside the tryCatch, as otherwise the error occurs once the returned plot object is automatically printed.

    I would still be interested in the reverse equivalent of options(warn=2) (i.e. instead of turning warnings into errors so that they can be traced, it would turn errors into warnings, so they don't generate a huge traceback).