Error Reporting in R

R supports global error handling, making it easy to report all errors without individual tryCatch statements.

Create a file to source at the start of all your scripts.

if (!interactive()) {
  options(error = function() {
    message <- geterrmessage()

    ### your error reporting goes here

    write("Execution halted", stderr())
    q("no", status = 1, runLast = FALSE)

Unfortunately, there’s no way to get filenames and line numbers (if you manage to do this, let me know!). Thankfully, the last line of the messages includes calls.

Error in func3(b) : unused argument (b)
Calls: func1 -> func2 -> func3

Happy production debugging! dolphin

If you use Rollbar...

Check out the Rollbar package.

Published May 11, 2016 · Tweet

You might also enjoy

R and Database URLs

Postgres SSLMODE Explained

Active Storage S3 Client-Side Encryption

All code examples are public domain.
Use them however you’d like (licensed under CC0).