简体   繁体   English

即使图形更新,R Shiny ggvis工具提示也不会使用新数据更新信息

[英]R shiny ggvis tooltip will not update information with new data, even though plot updates

So I have a ggvis plot within a shiny application, which is a histogram that that displays the count of observations in a bin when the user hovers over the bin. 因此,我在闪亮的应用程序中有一个ggvis图,该图是一个直方图,当用户将鼠标悬停在垃圾箱上时,它会显示垃圾箱中的观察计数。 The data is randomly generated and the user can hit a button to sample a new batch of data. 数据是随机生成的,用户可以单击按钮以采样新一批数据。 Everything works fine except that after a new batch of data is sampled and plotted, the tooltip still displays the counts in each bin from the original data, which of course does not match with the current data. 一切工作正常,除了在采样并绘制了一批新数据之后,工具提示仍会显示原始数据在每个bin中的计数,这当然与当前数据不匹配。 Is there a way to fix this? 有没有办法解决这个问题? Below is my minimal reproducible example. 以下是我的最小可复制示例。

toolTipFunc <- function(value) {
  function(y) {
    if (is.null(y)) return(NULL)
    else {
      length(value[(value >= y$xmin_) & (value < y$xmax_)])
    }
  }
}


library(shiny)
library(ggvis)

runApp(list(

ui = pageWithSidebar(
div(),
sidebarPanel(
   actionButton("sampleButton", "Sample")),

 mainPanel(
   ggvisOutput("plot"))),

 server = function(input, output) {

   sampleInput <- reactive({
     input$sampleButton
     x <- rnorm(500,0,1.5)
     data.frame(x)
   })

   gv <- reactive({
     df <- sampleInput()
     df %>% ggvis(~x) %>% 
       add_tooltip(toolTipFunc(df$x), "hover") %>% 
       layer_histograms(width=0.5) %>%
       set_options(width=540,height=350,keep_aspect=TRUE)
   })

   gv %>% bind_shiny("plot")

 }))

There was a working solution to this on the google ggvis group here . 此处的google ggvis组中有一个可行的解决方案。 Essentially you don't need your verbose, separate tool_tip function. 本质上,您不需要冗长的单独的tool_tip函数。 You can just use the following if using the default stack = TRUE 如果使用默认stack = TRUE则可以使用以下内容

gv <- reactive({
  df <- sampleInput()
  df %>% ggvis(~x) %>% 
    layer_histograms(width=0.5) %>%
    add_tooltip(function(df){ df$stack_upr_ - df$stack_lwr_}) %>%
    set_options(width=540,height=350,keep_aspect=TRUE)
})

or if stack=FALSE 或者如果stack=FALSE

gv <- reactive({
  df <- sampleInput()
  df %>% ggvis(~x) %>% 
    layer_histograms(width=0.5, stack=FALSE) %>%
    add_tooltip(function(df){ df$count_}) %>%
    set_options(width=540,height=350,keep_aspect=TRUE)
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM