Site icon R-bloggers

Keep your team informed with “slackr”

[This article was first published on Data Driven Security, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Karl Broman did a spiffy job summarizing a good number of the options available to R folk if they want to get notifications from R. You can also generate OS X notifications as well. If you’re using Slack for team coordination and communications, you’ve got a new option – slackr that also enables you go a bit deeper than just notifications, letting you push R output to the service for sharing results or observations.

What is Slack?

Slack (@SlackHQ) is a cloud-hosted, team messaging platform that lets you setup public & private internal channels for various types of communications, incuding markdown, text, pictures, video, links and more. They also offer connectivity with many outside services (e.g. github, twitter, etc.). The service is painless to setup, and there are desktop and mobile applications for most platforms (as well as the web interface). It has the utility of e-mail, twitter and skype (and more) combined, and their API makes it possible to create your own integrations.

While their full API affords more flexibility, they have simple “webhook”-type integrations that are more lightweight and make quick work out connecting to Slack. The slackr package takes advantage of the webook API to connect R with the service. To use it, you’ll first need to signup for the service and get your teammates to join and then setup the webhook integration.

Why slackr?

If you’ve ever used a plaintext messaging tool (e.g. Skype) to try to share R code snippets or output, you know the drill: select what was sent to you; copy, then paste into a text editor so it’s actually readable. The slackr package eliminates those steps by letting you execute one function – slackr() – and send any R output/expression to any Slack team channel or team member. Here’s an example, using the code from the lm stats package function example code (hit up ?lm in R to see that directly):

# setup the slackr API. this example assumes a .slackr config file in ~/
slackrSetup()

# run the lm() example
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)
lm.D9 <- lm(weight ~ group)
lm.D90 <- lm(weight ~ group - 1) # omitting intercept

# share the results with Jay
slackr(anova(lm.D9), summary(lm.D90), channel="@jayjacobs")

Here’s what will be seen in the slack channel:

The slackr() function can also be setup to do trivial notifications (and the various Slack apps and integrations can notify you anywhere in an out of slack, if that’s your cup of tea):

performSomeLongClassifictationTask()

# notify me directly
slackr("Classification complete", channel="@hrbrmstr")

# or notify the default channel
slackr("Classification complete")

With slackrSetup(), you can choose the default channel and username, as well as select the icon being used (overriding the default one during the initial webhook setup). The config file (mentioned earlier) is pretty straightforward:

token: YOUR_SLACK_API_TOKEN
channel: #general
username: slackr
icon_emoji: :information_source:
incoming_webhook_url: https://YOURTEAM.slack.com/services/hooks/incoming-webhook?

and definitely beats passing all those in as parameters (and, doesn’t have to live in ~/.slackr if you want to use an alternate location or have multiple profiles for multiple teams).

The webhook API is text/rich-text-only, but the full API lets you send anything. For that, full OAuth setup is required, and since it’s super-simple to just drag graphics from an RStudio window to Slack the extra functionality hasn’t made it to the slackr project TODO list yet, but I can defintely see a ggslack() or even a dev.slack() graphics device (ala png()) function or two making their way to the package in the not-too-distant future.

How to get slackr

The slackr package is up on github and may make it to CRAN next week. Over the coming weeks we’ll probably add the ability to consume output from slack channels and definitely welcome any issues, comments or feature requests.

To leave a comment for the author, please follow the link and comment on their blog: Data Driven Security.

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.