Site icon R-bloggers

rbokeh Version 0.4.1 Released

[This article was first published on Ryan Hafen, 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 rbokeh package version 0.4.1 was recently released.

< !--more-->

The most major addition in 0.4 is support for javascript callbacks for custom interactivity, which I’ll provide an example of below and will be posting about in more detail soon. Thanks to initial work on this from Jonathan Owen and recent help and motivation from Saptarshi Guha.

In addition to JS callbacks, additional improvements were made to non-standard evaluation from Barret Schloerke. Many other minor improvements were made, which are listed below in detail.

A CRAN release is imminent, but for now you can install the latest version with the following:

install.packages("rbokeh", repos = c(CRAN = "http://cran.rstudio.com",
  tessera = "http://packages.tessera.io"))

Callback support

Javascript callbacks allow you to specify custom behavior to be triggered when a user performs certain interactions with your rbokeh plots. Currently callbacks can be triggered by range change events (through panning / zooming), selection events (by clicking or box/lasso-select), and hover events.

Callback support has been a feature of Bokeh for a while but we’ve been working on the best way to expose it in the R interface. This should be considered an experimental feature for rbokeh, in that we may slightly change the interface after getting more use cases in order, but we are exposing it so that people can start playing with it.

Here is an example. I’ll go into detail about how these work and how can make your own in future posts.

dat <- data.frame(x = runif(500), y = runif(500))

p <- figure(title = "select points to adjust mean line",
  tools = "lasso_select") %>%
  ly_points(x, y, data = dat, lname = "points") %>%
  ly_lines(x = c(0, 1), y = rep(mean(dat$y), 2), line_width = 6,
    color = "orange", alpha = 0.75, lname = "mean")

code <- "
  var inds = cb_obj.get('selected')['1d'].indices;
  var d = cb_obj.get('data');
  var ym = 0;

  if (inds.length == 0) { return; }

  for (i = 0; i < inds.length; i++) {
    ym += d['y'][inds[i]];
  }
  ym /= inds.length;

  mean_data.get('data').y = [ym, ym];

  cb_obj.trigger('change');
  mean_data.trigger('change');
"

p %>% tool_lasso_select(custom_callback(code, "mean"), "points")

Another noteworthy addition is callbacks that allow plot interaction events to trigger Shiny application events, so that you can get more interactivity out of rbokeh plots in your Shiny apps without writing a line of javascript. More on this soon!

For the impatient, you can find several examples here, here, here, here, and here.

List of updates

To leave a comment for the author, please follow the link and comment on their blog: Ryan Hafen.

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.