Error notifications from R

I’m enthusiastic about having R notify me when my script is done.

But among my early uses of this, my script threw an error, and I never got a text or pushbullet about that. And really, I’m even more interested in being notified about such errors than anything else.

It’s relatively easy to get notified of errors. At the top of your script, include code like options(error = function() { } )

Fill in the function with your notification code. If there’s an error, the error message will be printed and then that function will be called. (And then the script will halt.)

You can use geterrmessage() to grab the error message to include in your notification.

For example, if you want to use RPushbullet for the notification, you could put, at the top of your script, something like this:

options(error = function() { 
                    pbPost("note", "Error", geterrmessage())

Then if the script gives an error, you’ll get a note with title “Error” and with the error message as the body of the note.

Update: I knew I’d heard about this sort of thing somewhere, but I couldn’t remember where. Duh; Rasmus mentioned it on twitter just a couple of days ago! Fortunately, he reminded me of that in the comments below.

Another update: Ian Kyle pointed out in the comments that the above function, if used in a script run with R CMD BATCH, won’t actually halt the script. The simplest solution is to add stop(geterrmessage()), like this:

options(error = function() { 
                    pbPost("note", "Error", geterrmessage())
                    if(!interactive()) stop(geterrmessage())

Tags: , ,

7 Responses to “Error notifications from R”

  1. Peter Hickey (@PeteHaitch) Says:

    Cool! This may well save me lost overnight computing time by notifying me of errors early enough so that I don’t come into uni the next day to find I have no results, only errors.

    One thing I still need to figure out is how to turn off pushbullets during certain hours, i.e. while I’m asleep. During my early experiments with it I was woken by my phone receiving a ping about a script completing.

  2. rasmusab Says:

    As I mentioned on twitter, the following makes R scream every time there is an error:

    options(error = function() { beep(9) } )

    Fun but super irritating, maybe something to put in your colleague’s .Rprofile? 🙂

  3. Ian Kyle Says:

    The only issue I have is that my scripts don’t halt on an error. Maybe a re-write of the system.time function could be useful. Enclosing a script in a call to system.time2 would send you a notification when the script has completed or if you have an error, you would be notified of that error and your script would halt.

    system.time2 <- function(expr, device=1){
    ppt <- function(y) {
    if (![4L]))
    y[1L] <- y[1L] + y[4L]
    if (![5L]))
    y[2L] <- y[2L] + y[5L]
    if (!exists("proc.time"))
    return(rep(NA_real_, 5L))
    time <- proc.time()
    on.exit({cat("Timing stopped at:", ppt(proc.time() – time),
    new.time <- proc.time()
    body=paste('expression took ', round((new.time – time)/ 60, 2), ' minutes'),

    structure(new.time – time, class = "proc_time")

    ### use ###
    # send a push 10 seconds later that everything finished

    # on error, send push with the error

  4. system.done = system.time + pbPost | The woRst Says:

    […] I love Pushbullet and I love R, so when Dirk Eddelbuettel’s brilliant package RPushbullet showed up on CRAN, I was rather intrigued. Then Karl Broman followed up with RPushbullet for error notifications from R: […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s