I would like to make the tabPanel change in Shiny when I click on a Leaflet polygon. I have a couple of ideas on how to do this, but I can't find the information I need to implement them. I have the leaflet in a tabPanel, but I would like to switch to another tab when a polygon is clicked on.
leaflet(llmap) %>%
addTiles() %>%
addPolygons(stroke = F,
fillOpacity = .8,
smoothFactor = .5,
color=~pal(x),
popup = pop)
I thought of making popup=updateTabsetPanel(session="New Tab")
, but that doesn't work. My other idea is to call updateTabsetPanel(session="New Tab")
anytime the user clicks on a new polygon, but I don't know what event I would need to return to let it know that a new polygon was clicked or even if a new popup popped up. Does anyone know this?
Here's an example of a reactive function that updates when you click a polygon:
output$myMap <- renderLeaflet({
map_out() #this is just a function that returns a leaflet map
})
output$MyGraph <- renderPlot({
event <- input$myMap_shape_click #Critical Line!!!
... #Code to run here
GraphData <- GraphData[event$id] # subsetting example
}
})
A few things to note here:
the input$myMap_shape_click
changes based on what you call your map above. I called it myMap
, so the structure is as shown. If you used output$YourMap
to initialize, the click would be called with input$YourMap_shape_click
the id of the polygon that you click on can be accessed with event$id
. This can be really useful for subsetting + graphing based on a polygon that is clicked. Also accessible are event$lat
and event$lng
the renderPlot
can be any reactive function. If its not tied to a specific output, you can just use observe
as below. This way, your code will run anytime a polygon is clicked. This is because the value of input$myMap_shape_click
changes every time you click.
I haven't used updateTabsetPanel
before, but I'd imagine this will work:
observe({
event <- input$myMap_shape_click
updateTabsetPanel(session, "inTabset", selected = event$id)
})
which would switch the tab to a panel with the same id as the polygon you clicked.