What I want to do is pretty simple. I want to be able to save all click events on a Shiny/Leaflet map. Here's some example 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
As you can see, I can print the click ids (or entire click event) when I click on a polygon. Easy enough. However, the moment I click another polygon, all information about my first clicked polygon is lost. I see that there is an argument option of autoDestroy = F
in observeEvent
, but I'm not sure how I would use this to save previously clicked polygons. Is there a way that I can store ALL of my clicks/click$ids in a vector or list?
You can do this using
reactiveValues
to store the clicks.Right at the top of your server function add
and then change your
observeEvent
to:What happens is every time you click, the
id
is appended to thelist
of clicks stored inRV$Clicks
. This does not have to be alist
you could make it avector
if that is better for you.