rbokeh hover tooltips - NSE getting in the way

129 views Asked by At

I'm trying to make a customized tooltip in rbokeh but when I try to do it programatically the non-stadard evaluation gets in the way.

From the example:

library(rbokeh)

mtcars$model <- row.names(mtcars)
figure() %>%
  ly_points(disp, mpg, data = mtcars, color = cyl,
            hover = "This <strong>@model</strong><br>has @hp horsepower!")

Rbokeh helpfully fills in @model and @hp with the variables when hovering. However, when I try to make the hover use a character string that I can change on the fly, for example:

hover_text <- "This <strong>@model</strong><br>has @hp horsepower!"
mtcars$model <- row.names(mtcars)
figure() %>%
  ly_points(disp, mpg, data = mtcars, color = cyl,
            hover = hover_text)

rbokeh doesn't correctly fill in the variables in the tooltip.

How can I get rbokeh to treat hover_text the same as the original character string?


I've tried a couple of variations of do.call, but all of them have had errors.

ly_points_docall <- function(...) {
  do.call(ly_points, list(..., hover = hover_text))
}

figure() %>%
  ly_points_docall(disp, mpg, data = mtcars, color = cyl,
                   hover = hover_text)
#  Error in do.call(ly_points, list(..., hover = hover_text)) : 
#  object 'disp' not found 

And

ly_points_docall <- function(...) {
  do.call(ly_points, list(..., hover = hover_text))
}

figure() %>%
  ly_points_docall(~disp, ~mpg, data = mtcars, color = ~cyl,
                   hover = hover_text)
# Error in (function (fig, x, y = NULL, data = figure_data(fig), glyph = 21,  : 
#   formal argument "hover" matched by multiple actual arguments 
1

There are 1 answers

1
GregF On BEST ANSWER

figured it out, pryr::subs() and eval() were the key.

pryr::subs(
  figure() %>% 
    ly_points("disp", "mpg", data = mtcars, color = "cyl",
              hover = hover_text)
) %>% 
  eval()