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() { 
                    library(RPushbullet)
                    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() { 
                    library(RPushbullet)
                    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:

    library(beepr)
    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 (!is.na(y[4L]))
    y[1L] <- y[1L] + y[4L]
    if (!is.na(y[5L]))
    y[2L] <- y[2L] + y[5L]
    y[1L:3L]
    }
    if (!exists("proc.time"))
    return(rep(NA_real_, 5L))
    time <- proc.time()
    on.exit({cat("Timing stopped at:", ppt(proc.time() – time),
    "\n")
    if(require(RPushbullet){
    pbPost(type='note',
    title='Error',
    body=geterrmessage(),
    recipients=device)
    })
    }
    expr
    new.time <- proc.time()
    on.exit()
    if(require(RPushbullet)){
    pbPost(type='note',
    title='Done!',
    body=paste('expression took ', round((new.time – time)/ 60, 2), ' minutes'),
    recipients=device)
    }

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

    ### use ###
    # send a push 10 seconds later that everything finished
    system.time2({
    Sys.sleep(5)
    Sys.sleep(5)
    })

    # on error, send push with the error
    system.time2({
    Sys.sleep(5)
    this.function.doesnt.exist(x)
    })

    • Karl Broman Says:

      Interesting; thanks for pointing this out. It seems that options(“error”) behaves differently in interactive mode vs in batch.

      My solution would be to add stop(geterrormessage()) to the function, as in this gist.

  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: […]

Comments are closed.