Connecting R to Everything with IFTTT
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
IFTTT (“if this then that”) is one of my favorite tools. I use it to keep and share articles, turn on my home’s lights at sundown, alert me when certain keywords are mentioned on Twitter/Reddit/etc., and many other things. Recently, the great people at IFTTT announced the Maker Channel, which allows recipes to make and receive web requests. This second option caught my interest as a nice way to do all kinds of things from R. For example, you could set the temperature with a NEST Thermostat, blink your HUE lightbulbs, write some data to a Google Drive document, or do a [whole lot(https://ifttt.com/channels) of other things.
For the sake of demonstration, I’m going to use the Maker Channel to send notifications to my phone (even though I’m partial to the pushoverr package for that).
If you don’t already have one, you’ll first need to create an IFTTT account. To receive notifications on your phone, you’ll also need to install the IF app for iOS or Android. The instructions that follow can either be done from the IF app or from the IFTTT website. I’m going to be using the website.
Setting Up the Maker Channel
First, we’ll need to visit the Channels page to enable the Maker Channel. In the search bar, type “maker“. Select it by clicking on the fancy M in the results.
Now activate the Maker Channel by clicking on the Connect button.
This will get the channel ready to go and generate a secret key. You can see in the picture below that my secret key is ci740p2XeuKq35nfHohG9Z
. It’s called a secret key for a reason, so don’t share this with anyone, or they can use it to trigger whatever actions you define. If you’ve managed to leak yours like I have here, you can generate a new secret key by pressing the Reconnect Channel button (which I have done following this post, so don’t try to send me your ANOVA results).
Now we’re ready to go. If you’d like to see exactly how to trigger events with the Maker Channel, follow the How to Trigger Events link (if you’ve clicked the link here, you’ll need to replace REPLACE_ME with your actual secret key).
Creating a Recipe for Notifications
Now we’re going to create a recipe to send you a notification on your phone whenever you (or R!) connects to the Maker Channel. Go to My Recipes and click on the Create a Recipe button.
If you’re new to IFTTT, the goal is to connect some output from one channel (i.e., the this) with some other channel (i.e., the that).
Click on the this link, and then select the fancy M for the Maker Channel.
For the this part of a recipe, we can only choose to receive web requests, so follow that option. We’re now going to pick a name for the event that is triggered whenever a web request is received. For now, let’s call it r_status. When you’re a Maker Channel pro, you can create multiple different events and have them do different things.
Now it’s time to choose the that part of the recipe, which is what IFTTT will do whenever it receives r_status events. Click on the that link, and select either Android Notifications or iOS Notifications, depending on which type of device you have. I’ll be going with the iOS option, so the remaining screenshots may look slightly different for you.
No matter which route you go, the next option is to select Send a Notification.
Here, we can get creative with what the notification says. You can include the name of the event ({{EventName}}), when the web request was received ({{OccurredAt}}), and up to three text values that are given in the web request ({{Value1}}–{{Value3}}). So if you’re fitting a linear model with lm
, you could notify yourself of the slope, the intercept, and the amount of time it took to run, which we’ll do later.
Once you’ve crafted your message, hit the Create Action button, edit your recipe title (optional), and hit the Create Recipe button.
Giving R a Voice
Now fire up R or RStudio. We’re going to need the httr package to send web requests. If you don’t already have it (or think you might be out of date), run install.packages("httr")
.
Before we send our first notification, I’m going to save my event name and secret key in variables. If you do the same (but with your secret values), you’ll be able to easily copy and run all of the other code that follows.
my_event <- 'r_status' my_key <- 'ci740p2XeuKq35nfHohG9Z'
Now let's send a first message! First, we'll build the URL where we'll issue the request, and then we'll issue the request with httr's POST
:
maker_url <- paste('https://maker.ifttt.com/trigger', my_event, 'with/key', my_key, sep='/') httr::POST(maker_url) ## Response [https://maker.ifttt.com/trigger/r_status/with/key/ci740p2XeuKq35nfHohG9Z] ## Date: 2015-06-18 19:01 ## Status: 200 ## Content-Type: text/html; charset=utf-8 ## Size: 48 B
If all went well, your device should be beeping or buzzing. We see in the results that the status was 200, which indicates a success.
Sending Values
You may have noticed that your notification didn't contain any data. We can add data by specifying values for value1
, value2
, and value3
(note the lowercase) in the body of our message.
httr::POST(maker_url, body=list(value1='hola', value2='mundo', value3=7)) ## Response [https://maker.ifttt.com/trigger/r_status/with/key/ci740p2XeuKq35nfHohG9Z] ## Date: 2015-06-18 19:01 ## Status: 200 ## Content-Type: text/html; charset=utf-8 ## Size: 48 B
Now that ¡Hola, mundo! is out of the way, we can send it some data. I mentioned fitting a linear model earlier, so let's do that. First, we'll borrow some example code from lm
's help page and fit a linear model:
# Create some data ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69) group <- gl(2, 10, 20, labels = c("Ctl","Trt")) weight <- c(ctl, trt) # Fit the model lm.D9 <- lm(weight ~ group)
Now, we'll send a notification containing the model's slope and intercept:
httr::POST(maker_url, body=list(value1='lm complete', value2=coefficients(lm.D9)[[2]], value3=coefficients(lm.D9)[[1]])) ## Response [https://maker.ifttt.com/trigger/r_status/with/key/ci740p2XeuKq35nfHohG9Z] ## Date: 2015-06-18 19:01 ## Status: 200 ## Content-Type: text/html; charset=utf-8 ## Size: 48 B
Here, we've sent a little message as value1
, the slope as value2
, and the intercept as value3
.
Hopefully, this little example has demonstrated how IFTTT's Maker Channel can be used to connect R to a whole lot of online services. Have at it!
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.