楼主: oliyiyi
1375 0

Connecting R to Everything with IFTTT [推广有奖]

版主

已卖:2994份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
84105 个
通用积分
31671.0967
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2015-6-23 20:51:13 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
(This article was first published on Brian Connelly » R, and kindly contributed to R-bloggers)

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 IFTTTannounced 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 Recipebutton.

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 oriOS 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/ ... 40p2XeuKq35nfHohG9Z]##   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/ ... 40p2XeuKq35nfHohG9Z]##   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 datactl <- 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 modellm.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/ ... 40p2XeuKq35nfHohG9Z]##   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!


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Connecting Everything Connect Thing Every published Recently receive interest Twitter

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-31 08:51