ggvis icon indicating copy to clipboard operation
ggvis copied to clipboard

Odd behaviour with add_tooltip if a ggvis is bound reactively

Open ttzhou opened this issue 7 years ago • 0 comments

  1. Run the following
library(tidyverse)
library(shiny)
library(ggvis)

server <- function(input, output, session) {
	
	vis_data <- reactive({
		mtcars
	})
	
	output$vis <- renderUI({
		ggvisOutput('vis_test')
	})
	
	observeEvent(input$slider, {
		reactive({
			vis_data()[seq(1, input$slider), ] %>%
			ggvis(
				x = ~wt,
				y = ~mpg
			) %>%
			layer_points() %>%
			layer_bars() %>%
			add_tooltip(
				function(x) print(nrow(vis_data()[seq(1, input$slider), ]))
			)
		}) %>% bind_shiny('vis_test')
	})
		
}

ui <- function(input, output, session) { 
	fluidPage(
		sliderInput('slider', min = 1, max = nrow(mtcars), value = 3, label = 'sliderlabel'),
		htmlOutput(outputId = 'vis')	
	)
}

shinyApp(ui = ui, server = server)
  1. Hover over one of the bars, note the console/printed output. Should only print once
  2. Change the slider, hover over a bar. Now it prints twice!
  3. Change the slider again. Hover over a bar. Now it prints three times!

My hunch is that every time bind_shiny is called, the observer for add_tooltip isn't being destroyed, and each of these is observers is being called when the hover occurs.

Is there any workaround?

ttzhou avatar Jun 27 '18 04:06 ttzhou