Site icon R-bloggers

Extending slackr

[This article was first published on Rstats on Reproducible Random Thoughts, 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.

The slackr package, by Bob Rudis, contains functions that make it possible to interact with the Slack messaging platform. The great part of Slack is that it has mobile devices application interface. We take advantage of this by extending slackr’s interaction with with the server:

This lets us interact with R and Slack in new ways, by getting active updates on long simulations directly to your (and your team’s) mobile device and multitask away from your computer.

devtools::install_github('hrbrmstr/slackr')

Progress Bars

Create text progress bar that is sent directly to a Slack channel.

progress_slackr <- function(x){
  text_slackr('0%')
  s <- c(0,x,1)
  for(i in 1:length(s)){
    Sys.sleep(0.5)
    edit_slackr(sprintf('%s%%',round(100*i/length(s),1)))
  }
  Sys.sleep(0.5)
}

set.seed(1234)
progress_slackr(sort(runif(10)))

This also solves a long standing problem of tracking the progress of parallel jobs on a server. We use slackr with the qapply package, which runs jobs on an Open Grid Scheduler/Engine. We can track each node

Onexit

Attach on.exit expressions to any function in R and at the end of the original function an output will be sent to the Slack channel.

This is useful for letting you know when a simulation is done, but also to be able to send to the Slack channel a relevant summary to see that the simulation did as intended.

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)

#pass a message to Slack channel 'general'
register_onexit(lm,'bazinga!',channel="#general")

lm.D9 <- slack_lm(weight ~ group)

#test that output keeps inheritance
summary(lm.D9)

#pass a message to Slack channel 'general' with a header message to begin output
register_onexit(lm,'bazinga!',
channel="#general",
header_msg='This is a message to begin')

lm.D9 <- slack_lm(weight ~ group)

#onexit with an expression that calls lm.plot
register_onexit(lm,{
 par(mfrow = c(2, 2), oma = c(0, 0, 2, 0))
 plot(z)
},
channel="#general",
header_msg = 'This is a plot just for this output',
use_device = TRUE)

lm.D9 <- slack_lm(weight ~ group)

#clean up slack channel from examples
delete_slackr(count = 6,channel = '#general')

To leave a comment for the author, please follow the link and comment on their blog: Rstats on Reproducible Random Thoughts.

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.