How to “save” click events in the Shiny Map brochure

What I want to do is pretty simple. I want to save all click events on a Shiny / Leaflet map. Here is a sample code:

library(raster)
library(shiny)
library(leaflet)

#load shapefile
rwa <- getData("GADM", country = "RWA", level = 1)

shinyApp(
  ui = fluidPage(
    leafletOutput("map")
  ), 

  server <- function(input, output, session){

    #initial map output
    output$map <- renderLeaflet({
      leaflet() %>% 
        addTiles() %>% 
        addPolygons(data = rwa, 
                    fillColor = "white", 
                    fillOpacity = 1, 
                    color = "black", 
                    stroke = T, 
                    weight = 1, 
                    layerId = rwa@data$OBJECTID, 
                    group = "regions")
    }) #END RENDER LEAFLET

    observeEvent(input$map_shape_click, {

      #create object for clicked polygon
      click <- input$map_shape_click

      print(click$id)

    }) #END OBSERVE EVENT
  }) #END SHINYAPP

enter image description here

As you can see, I can print out the click IDs (or the whole click event) when I click on the polygon. Easy enough. However, the moment I click on another polygon, all information about my first clicked polygon is lost. I see that there is an argument parameter autoDestroy = Fin observeEvent, but I'm not sure how to use it to save previously clicked polygons. Is there a way to save all my clicks / click $ ids in a vector or list?

+4
1

, reactiveValues, .

RV<-reactiveValues(Clicks=list())

observeEvent :

observeEvent(input$map_shape_click, {

      #create object for clicked polygon
      click <- input$map_shape_click
      RV$Clicks<-c(RV$Clicks,click$id)
      print(RV$Clicks)

 }) #END OBSERVE EVENT

, id list , RV$Clicks. list, vector, .

+5

Source: https://habr.com/ru/post/1663542/


All Articles