rbokeh悬停工具提示 - NSE阻碍

时间:2017-01-03 16:47:21

标签: r lazy-evaluation bokeh rbokeh

我试图在rbokeh中制作一个自定义的工具提示但是当我尝试以编程方式进行时,非标准评估会妨碍。

来自示例:

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在悬停时有助于填充@model@hp变量。但是,当我尝试使用悬停时,我可以动态更改字符串,例如:

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没有正确填写工具提示中的变量。

如何让rbokeh将hover_text视为与原始字符串相同?

我尝试了do.call的几种变体,但所有变体都有错误。

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 

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 个答案:

答案 0 :(得分:0)

想通了,pryr::subs()eval()是关键。

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