I would like to build a shiny app that has a popup show up when the mouse goes over a shape/circle rather than the standard click
in particular i am trying to get the popup show as the mouse hovers over...and it disappears as the mouse moves away from it.
This page (https://rstudio.github.io/leaflet/shiny.html) would suggest i need to have something like an observeEvent({input$mymap_shape_mouseover},{showPopup()})
but not sure where to enter it or how to use it, so any help would be much appreciated.
Below is a simple random example...
library(shiny)
library(leaflet)
library(data.table)
uu <- data.table(row_num=seq(100),
Latitude=c(52+cumsum(runif(100,-0.001,0.001))),
Longitude=c(1+cumsum(runif(100,-0.001,0.001)))
)
ui <- fluidPage(
leafletOutput("mymap")
)
server <- function(input, output, session) {
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addCircles(lng=uu$Longitude,
lat=uu$Latitude,
radius=2)
})
# Show a popup at the given location
show_popup_on_mouseover <- function(id, lat, lng) {
selected_point <- uu[row_num == id,]
content <- as.character(selected_point$row_num)
leafletProxy("mymap") %>%
addPopups(lng, lat, content)
}
# When circle is hovered over...show a popup
observe({
leafletProxy("mymap") %>% clearPopups()
event <- input$mymap_shape_mouseover
print(event)
if (is.null(event)){
return()
} else {
isolate({
show_popup_on_mouseover(event$id, event$lat, event$lng)
})
}
})
}
shinyApp(ui, server)