DO Something Nifffty with R

@briandconnelly (of pushoverr fame) made a super-cool post about connecting R to @IFTTT via IFTTT’s “Maker” channel. The IFTTT Maker interface to receive events is fairly straightforward and Brian’s code worked flawlessly, so it was easy to tweak a bit and wrap into a package.

To get started, you can clone an example public IFTTT recipe that posts data to a Dropbox folder. All you need to do is add the IFTTT channel to your account and add the API key to your .Renviron file, which will look something like:

IFTTT_API_KEY=uGlySTringOfCharACTers-

Sticking it in the .Renviron file is a nice platform-independent way of getting the variable in your environment (and you can set a similar environment variable in Travis for automated testing). NOTE that you’ll need to restart your R/RStudio session for that value to become available (what, you don’t have RStudio up all the time?).

Once you have the key in place and the IFTTT recipe setup, all you have to do is:

devtools::install_github("hrbrmstr/nifffty")
library(nifffty)
maker("rtest", "this", "is a", "test")

and you’ll have a file named something like june_19__2015_at_0927am.txt in a Dropbox folder that should have values like:

Value 1: this
Value 2: is a
Value 3: test

The potential IFTTT integrations are not limited to Dropbox (or iOS/Android notification as in Brian’s example). You can literally trigger anything from R in the IFTTT catalogue. Go foRth and automate!

DOing even MoRe

As stated, Brian’s code was complete, so making a package to post to IFTTT’s Maker channel was really easy. But, this is R and if we can get data to it, things can be even cooler. So, I made a receiver function that will catch Maker channel web requests and do stuff with them. Since I have an Apple Watch, I decided I wanted to test the interface out with IFTTT’s DO button and have R receive my coordinates whenever I press this:

IMG_0853

(That’s an actual screen capture from the Apple Watch).

If you have both R and an Apple Watch, you can create a DO app as such (NOTE that DO apps work fine on the iPhone without an Apple Watch):

do_button_r_nifffty

That will POST a bit of JSON to whatever is listening for it. To get R to listen to it, you need to build a small script that I’ll call listen.R, create a function to process the data and register it with the instance of the Rook web server that it will automagically create for you. You can specify which IP address and/or port to listen on as well. NOTE that this R server must be reachable from the internet, so you may need to do port forwarding if behind a NAT firewall.

My sample listen.R looks like this:

library(nifffty)
 
# not the best name for a function but it works
do_it <- function(req) {
  require(jsonlite)
  print(fromJSON(names(req$POST())))
  writeLines(names(req$POST()), "/tmp/bob.txt")
}
 
# you can change the port to match what you put into IFTTT
rcvr <- receiver(port=10999, handler=do_it)
 
# this is not necessary but it's a good diagnostic for a test script
print(rcvr)
 
# keeps the script alive
while (TRUE) Sys.sleep(24 * 60 * 60)

You can run that from a command-line (I tested it on Ubuntu) as such:

bob@server:~$ Rscript listen.R
Loading required package: methods
Loading required package: Rook
Server started on 0.0.0.0:10999
[1] nifffty http://0.0.0.0:10999/custom/nifffty
 
Call browse() with an index number or name to run an application.

When you tap the DO button on the Apple Watch, you’ll see the following in the console:

Loading required package: jsonlite
 
Attaching package: ‘jsonlite’
 
The following object is masked from ‘package:utils’:
 
    View
 
$latitude
[1] 43.25931
 
$longitude
[1] -70.80062
 
$timestamp
[1] "June 19, 2015 at 04:45PM"

and the following in the file it was instructed to write to:

bob@server:~$ cat /tmp/bob.txt
{ "latitude": 43.2593566552207, "longitude": -70.8004647307757, "timestamp": "June 19, 2015 at 04:46PM" }

JSON is a good choice given how easy it is to work with in R and you can have R do anything you can dream up with the data that’s sent to it. You can connect any supported IFTTT component and hook any variable from said component into the JSON that R will receive.

Coming Soon

The next step is to make a Shiny interface so it can receive and process real-time events. I’m hoping to make a sample Shiny app that will update my location on a map every time I tap the DO button and also compute some stats for it. I also need to put some polish on the receiver interface (add logging and some error checking).

If you have cool ideas or have hooked R up to IFTTT’s Maker channel to do something cool, drop a note in the comments and definitely submit any pull requests or issues on github.

Buy on AmazonDDS Blog
DDS PodcastAmazon Author Page

2 Comments DO Something Nifffty with R

  1. Pingback: DO Something Nifffty with R | infopunk.org

  2. Hendrik

    Hi, thanks for your post. I will try the instructions, but do not have an apple watch though. Between you and @briandconnelly, I am wondering how would I be able to capture the location of many mobiles in one go.

    Reply

Leave a Reply