4 min read

Notifications from R

You just sent a long R job running. How to know when it’s done? Have it notify you by beeping, sending you a text, or sending you a notification via pushbullet.

beepr

You can use Rasmus Baath’s beepr package to have your computer play a sound.

Install it from CRAN with

install.packages("beepr")

Then, at the bottom of your script, load the package and have it beep.

library(beepr)
beep()

beepr comes with a bunch of different sounds (e.g., try beep("facebook")), or use a path to any wav file. I tried these cat sounds, but they came out (with my computer’s speaker) sounding more like a really unhappy electronic lion…not very pleasant.

beepr is cool, but most of my long-running jobs are on a distant server. It could be useful for local jobs on my Mac, but most of the time my computer is muted or the sound goes to my headphones (and my headphones are not always on my head). So I’m instead having my scripts send me a text or a notification via pushbullet.

gmailR

There are a number of different packages for sending email from R (e.g., sendmailR and mailR). I tried Tyler Rinker’s gmailR package.

You can send yourself an email using the gmail() function. And since most cell phone companies have a method for sending a text via email, you can use this same function to send yourself a text. gmailR includes a function cell2email() that will help you figure out the appropriate email address to use, to send a text to your phone.

The only problem with gmailR is that you have to pass your gmail account and password to the gmail() function, and you don’t really want to have that stuff sitting in your R script.

So I wrote a little package mygmailR, that will instead pull your private information from a file (~/.gmail_private). I set up a separate gmail account for this purpose, and I set the security settings to use app-specific passwords.

My ~/.gmail_private file looks like the following: app-specific gmail password, gmail account to be the default “from” email address (perhaps special for this purpose), the email address to use to send a text, and the default “to” email (which is my usual gmail account).

password my_private_app_specific_password
gmail    my_gmail_account@gmail.com
text     0123456789@txt.att.net
to       default_to@gmail.com

You need to install a few packages. gmailR and mygmailR are not available on CRAN, so you need to install devtools and use the install_github() function to install them from GitHub.

install.packages(c("devtools", "rJython", "rJava", "rjson"))
library(devtools)
install_github("trinker/gmailR")
install_github("kbroman/mygmailR")

In your script, to send yourself a text, you’d write

library(mygmailR)
send_text("subject here" "body of message here")

To send yourself an email, write

library(mygmailR)
send_gmail("subject here", "body of message here")

RPushbullet

Another alternative (suggested to me by Peter Hickey and Jared Knowles) is to use Dirk Eddelbuettel’s RPushbullet package to send yourself a notification via pushbullet.

The main advantage of this, in my mind, is that there’s no gmail password sitting around on your system anywhere, but rather just your pushbullet “Access Token”, sitting in the file ~/.rpushbullet.json, so this is less of a security issue.

  1. Sign up for pushbullet; you’ll need a Google account.

  2. Install the pushbullet app on your phone or other device, or install the chrome extension.

  3. Go to your pushbullet account page to get your “Access Token”.

  4. Install the RPushbullet package from CRAN.

install.packages("RPushbullet")
  1. Create a ~/.rpushbullet.json file with your api key and not much else.
{
    "key": "your_api_key",
    "devices": [],
    "names": []
}
  1. Install jsonlite and use RPushbullet’s pbGetDevices() function to get the identifiers for the devices you’ve registered with pushbullet.
install.packages("jsonlite")
library(jsonlite)
library(RPushbullet)
fromJSON(pbGetDevices())$devices[,c("iden", "nickname")]
  1. Insert those device identifiers into your ~/.rpushbullet.json file.
{
    "key": "your_api_key",

    "devices": [
        "your_phone_device_id",
        "your_tablet_device_id",
        "your_chrome_device_id";
    ],

    "names": [
        "phone",
        "tablet",
        "Chrome";
    ]
}

Now you’re set! Use the pbPost() function to post a message to yourself.

library(RPushbullet)
pbPost("note", "Title of note", "Body of message")

By default, the message is posted to the first device listed in your ~/.rpushbullet.json file; to post it to a different device, use the argument deviceind, which takes a positive integer.

I’m still somewhat inclined towards using gmailR and mygmailR to send myself a text, but RPushbullet seems more secure.

Update: The word from Hadley: we should also check out gmailr. [But you can’t currently send email with gmailr.]