Closed Captioning Closed captioning available on our YouTube channel

How to send Slack messages with R

InfoWorld | Jun 19, 2019

In this episode of Do More With R, Sharon shows you how to send Slack messages using R. It may not seem helpful at first, but there's plenty of uses. You can even use it to notify yourself or others when a lengthy R script finishes running.

Similar
Hi. I’m Sharon Machlis at IDG Communications, here with episode 29 of Do More With R: Send a Slack message right from your R script.
Using R to send a Slack message may sound like a parlor trick, but there are actually a lot of cases where that could come in handy. I’ve used it to send graphs to colleagues with latest Web analytics trends. You can also use it to notify yourself or others when a lengthy R script finishes running.
Bob Rudis created an R package that makes it easy to send Slack messages with R. It’s called slackr and it’s on CRAN, so you can install it with install.packages().
The most complicated part of this by far is initial set-up. Fortunately, you only need to do that once. Let’s go through it step by step.
You’ll need to authorize R to use your specific Slack. Bob shows a sample configuration file to do this, in the following format
So, we need an API token and an incoming Web hook URL. Slack’s developer documentation for this can be tough to find. It turns out that you want to create a new Slack app in whatever Slack you want to message.

Go to api.slack.com/apps

Click the green Create an App button. Next, select “Incoming Web hooks” and then click the button to activate them.

Finally, scroll down and add a new Web hook. You’ll be asked what channel you want to post to. Don’t worry, you can override that, you don’t need a new hook for each channel. That’s just a default. Pick one, and then click install.

Copy the resulting Web hook URL into your R Slack configuration file.

We also need an API token. A Slack “legacy” token works for this. Slack will show you many warnings advising you not to use them, but they work just fine for this. Here’s how to get one.

Go to api.slack.com/custom-integrations/legacy-tokens

and you should see your Slack workspace listed. Click the Create token button. (I already have one here, so the button doesn’t appear). Copy the token and pop that in your authorization file, too.

Slack set-up is done!

Next up is configuring slackr to use the authorization file. That part is more straightforward: Just run the slackr_setup() function.

slackr_setup() defaults to expecting a file named .slackr in your home directory. If you want to use another file in another location, add its name – including full file path if it’s not in your working directory – to slackr_setup(). You can see here I’m using a file called .morewithr_slack in my current project directory.

You’ll need to run slackr_setup() at the start of each session where you’re using slackr.

Finally, the fun part!

I have a simple text message including my current system date and time saved to a variable my_message. To send it to Slack, I’ll run the slackr_msg() function. The first argument is the message text, and the second argument is the channel I want to send to. Notice the channel name has a pound sign in front of it. If you want to send someone a private message, set the channel to their Slack name with an @ sign in front.

It’s also easy to send an image file with the slackr_upload() command. As I did last episode for sending email from R, I’ll create a graph of monthly US unemployment and save it to the file “unemployment_graph.png.” (That’s not the important part, I just need a break from mtcars and iris sample data.)

I’ll send a text message to Slack explaining the graph to come, and then I’ll upload the file itself.

I’ll send the text message, like I did before. Next is the file upload with slackr_upload(). The first argument is the file name, next is the file headline, and finally the channels (you can send to more than one at a time).

There you have it: Slack messages from R!

That’s it for this episode, thanks for watching! For more R tips, head to the Do More With R page at go dot infoworld dot com slash more with R, all lowercase except for the R.
You can also find the Do More With R playlist on the YouTube IDG Tech Talk channel.
Hope to see you next episode!
Popular
Featured videos from IDG.tv