Notifications from R

[This article was first published on The stupidest thing... » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 Bååth’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    [email protected]
text     [email protected]
to       [email protected]

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.
  5. install.packages("RPushbullet")
    
  6. Create a ~/.rpushbullet.json file with your api key and not much else.
  7. {
        "key": "your_api_key",
        "devices": [],
        "names": []
    }
    
  8. Install jsonlite and use RPushbullet‘s pbGetDevices() function to get the identifiers for the devices you’ve registered with pushbullet.
  9. install.packages("jsonlite")
    library(jsonlite)
    library(RPushbullet)
    fromJSON(pbGetDevices())$devices[,c("iden", "nickname")]
    
  10. Insert those device identifiers into your ~/.rpushbullet.json file.
  11. {
        "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.


To leave a comment for the author, please follow the link and comment on their blog: The stupidest thing... » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)