Brian Connelly: httr Articles tagged 'httr' on Brian Connelly en-us http://bconnelly.net Mon, 28 Nov 2016 15:10:40 -0800 Mon, 28 Nov 2016 15:10:40 -0800 Jekyll v3.3.1 R Phone Home: Notifications with pushoverr Brian Connelly Wed, 23 Nov 2016 12:09:00 -0800 http://bconnelly.net/2016/11/R-phone-home/ http://bconnelly.net/2016/11/R-phone-home/ rhttrpushoverhowto There are a lot of times when it would be great if your computer talked to you. pushoverr allows you to send yourself or your group notifications from R. Instead of compulsively checking the status of a running job, you can just have R tell you when it’s done, if it failed, or what the results are.

]]>
There are a lot of times when it would be great if your computer talked to you. pushoverr allows you to send yourself or your group notifications from R. Instead of compulsively checking the status of a running job, you can just have R tell you when it’s done, if it failed, or what the results are.

To get started, download a Pushover client for your mobile device or desktop. Pushover is free for 7 days, but requires a $4.99 license after that. Once you’ve registered, you’ll get a user key, which looks like uQiRzpo4DXghDmr9QzzfQu27cmVRsG. You can find your user key either in the app’s settings or on Pushover’s website after logging in.

Pushover allows you to create “applications”, which are basically just different channels for your messages. You can also receive messages from “public applications” like GitHub (via a service hook), IFTTT, Zapier, and many others.

Creating Your Application

All that it takes to create an application dedicated to receiving messages from R is to visit the create a new application and enter a few fields. For this demo, I’m going to call it pushoverr (it can be anything you want, though). The other fields are optional, but I’m going to set an icon. The R Logo seems like a natural fit (scaled down to 72 pixels wide, download here). Now, whenever a message arrives, I’ll be able to know at a glance that it came from R.

Creating our Pushover application

Once you’ve agreed to the terms and clicked Create Application, you’ll be taken to the information page for your new application. Here, you can see some plots of the number of messages sent by your app both recently and over a longer period. You can also create subscription codes for your applications, which can allow other people to easily subscribe to notifications for that app. This is really handy for sending notifications to coworkers or even a wider audience. For now, what’s most important is your API Token/Key, which we’ll need to send notifications from this app. For my demo application here, the API token is avgk9kgxjp2e5xqnn3kzutvh43g27k.

pushoverr application information page

Sending Notifications

Now it’s time to start sending some messages. Start up an R session and install pushoverr:

install.packages("pushoverr")

Once that’s completed, we’re ready to let R start talking. Load the pushoverr package, and use the pushover function to craft and send your first message.

library("pushoverr")

pushover(message = "Mr. Watson--come here--I want to see you.")

Since this is your first time, pushover will ask you for your user key and app token. Don’t use mine.

setting pushoverr credentials (screenshot)

If everything worked, you should see a notification on your device very shortly.

our first pushoverr notification arrives (screenshot)

Using other arguments to pushover, you can set other aspects of your message, including sounds, links, and message priorities.

Your user key and app token are now saved for the rest of your session, so you can now send messages without having to enter them. Pushover offers 5 different message priorities, ranging from silent to emergency. Let’s send an emergency priority message, which will re-send every 30 seconds until it is acknowledged on your device.

pushover_emergency(message = "The kittens are awake, and they are ANGRY!")

Notification: The kittens are awake, and they are ANGRY! (screenshot)

Displaying Data on Your Wrist

I previously used Numerous to put Lake Union’s current temperature on my watch, which I scraped using R. It was a great way to know how shocking my daily swim would be. Numerous shut down earlier this year, so I was extremely excited to see that Pushover recently added the ability to push data to smaller screens.

pushoverr can now send several different types of data to your watch. For example, we can quickly display the current water temperature.

water_temp <- 52
update_glance(count = water_temp)

Watch displaying temperature (screenshot)

I won’t be swimming today.

We can also send numeric values as a percent, which is good for displaying progress. There’s also support for small bits of text:

update_glance(text = "hello woRld")

Watch displaying 'hello woRld' (screenshot)

Wrapping Up

Enabling R to send you bits of information can be extremely useful. I’ve used Pushover to do this for a few years, but there are several other alternatives. Pushbullet is a similar service that offers a few additional features, but does not yet have watch support. Dirk Eddelbuettel’s RPushbullet package allows you to interface with Pushbullet from R. You can also send notifications from IFTTT. Check out my previous blog post Connecting R to Everything with IFTTT to see how to send notifications and a whole lot more.

]]>
http://bconnelly.net/2016/11/R-phone-home/#comments
Connecting R to Everything with IFTTT Brian Connelly Thu, 18 Jun 2015 13:21:00 -0700 http://bconnelly.net/2015/06/connecting-r-to-everything-with-ifttt/ http://bconnelly.net/2015/06/connecting-r-to-everything-with-ifttt/ howtorifttthttr 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 of other things.

]]>
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 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.

Finding the Maker channel

Now activate the Maker Channel by clicking on the Connect button.

Connecting the Maker Channel

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).

Channel is now activated

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).

Select THIS

Click on the this link, and then select the fancy M for the Maker Channel.

Choosing 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.

Entering the Event name

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.

Select THAT

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.

Creating the notification message

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.

Our first notification

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

Notification from our hola mundo example

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.

Notification from our lm example

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!

]]>
http://bconnelly.net/2015/06/connecting-r-to-everything-with-ifttt/#comments