Hide tooltip initialized on = "click" in ggvis chart
When using the ggvis tooltip function with on = "hover", the tooltip disappears when the cursor leaves the data point trigger:
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "hover")
The on = "click" behavior is not so intuitive in my opinion. Clicking on an item opens the corresponding tooltip. However, it can be closed again by opening another tooltip in the plot.
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click")
I would expect the tooltip to close again when I click on the data point again or anywhere outside the tooltip.
Can this behavior be emulated? I experimented with hide_tooltip
but couldn't figure out how to get a brilliant session from the ggvis interactive graph.
Update 2015-01-15
@wch will update the behavior at ggvis 0.5
( https://github.com/rstudio/ggvis/issues/250 ). I'll be back when it's released.
source to share
For those coming here in the future, this is the answer I gave in a GitHub issue that will work: you can add one simple JavaScript line that will close the tooltip whenever a click is clicked.
In a brilliant app
library(shiny)
library(ggvis)
jscode <-
"$(function() {
$('#ggvis_plot').click(function(){ $('#ggvis-tooltip').hide(); });
})
"
shinyApp(
ui = fluidPage(
tags$script(jscode),
uiOutput("ggvis_ui"),
ggvisOutput("ggvis_plot")
),
server = function(input, output, session) {
mtcars %>%
ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click") %>%
bind_shiny("ggvis_plot", "ggvis_ui")
}
)
Note that the ID you pass to the function ggvisOutput()
must match the ID used in the JavaScript string, in this case I used id = ggvis_plot
.
In the interactive doc rmarkdown
---
title: "ggvis Hide Tooltip on Click"
runtime: shiny
output:
html_document
---
<script>
$(function() {
$('#ggvis_plot').click(function(){ $('#ggvis-tooltip').hide(); });
})
</script>
```{r echo = FALSE}
library(ggvis)
ggvis_plot <- reactive({
mtcars %>% ggvis(~wt, ~mpg) %>%
layer_points() %>%
add_tooltip(function(df) df$wt, on = "click")
})
invisible(bind_shiny(ggvis_plot, 'ggvis_plot'))
ggvisOutput('ggvis_plot')
```
Note that again the ID you pass to the function ggvisOutput()
must match the ID used in the JavaScript string, in this case I used id = ggvis_plot
.
source to share