How to create an event when pressing on a leaflet popup in R?

社会主义新天地 提交于 2019-12-06 00:52:50

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:

  1. 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

  2. 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

  3. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!