[英]R shiny ggvis tooltip will not update information with new data, even though plot updates
因此,我在闪亮的应用程序中有一个ggvis图,该图是一个直方图,当用户将鼠标悬停在垃圾箱上时,它会显示垃圾箱中的观察计数。 数据是随机生成的,用户可以单击按钮以采样新一批数据。 一切工作正常,除了在采样并绘制了一批新数据之后,工具提示仍会显示原始数据在每个bin中的计数,这当然与当前数据不匹配。 有没有办法解决这个问题? 以下是我的最小可复制示例。
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")
}))
此处的google ggvis组中有一个可行的解决方案。 本质上,您不需要冗长的单独的tool_tip
函数。 如果使用默认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)
})
或者如果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.