R Leaflet (CRAN) - how to register by clicking the marker

Using the RStudio Leaflet package in a brilliant application, I was able to fulfill all the functionality I was looking for, with the exception of deselecting the marker object after clicking it.

In particular, the input value of $ map_click_id is set to NULL before clicking any markers. When you click on a marker, it is updated with the data (ID, lat, lng, nonce) for that marker. I would like to configure the map so that when the user clicks on any area of ​​the map that is not a marker, enter $ map_click_id reset to NULL until another marker is pressed.

I tried a number of solutions to solve such problems, such as comparing click times for marker clicks and map clicks, but the marker click variable, when it is set to a value other than NULL, is updated every time the map is clicked, regardless whether it is on the marker or not, so this does not work.

Any help here would be greatly appreciated! The following is a very minimal reproducible example. In this case, I would like the marker information to be printed to the console when it is pressed, and for NULL - to the console when any marker area on the map is clicked.

library(leaflet) library(shiny) # set basic ui ui <- fluidPage( leafletOutput("map") ) server <- shinyServer(function(input, output) { # produce the basic leaflet map with single marker output$map <- renderLeaflet( leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addCircleMarkers(lat = 54.406486, lng = -2.925284) ) # observe the marker click info and print to console when it is changed. observeEvent(input$map_marker_click, print(input$map_marker_click) ) }) shinyApp(ui, server) 

This seems to be the same question as here , but since there was no answer to this question, I thought I would try again.

+4
source share
1 answer

You can use reactiveValues to hold the marker click and reset when the user clicks on the map background:

 server <- shinyServer(function(input, output) { data <- reactiveValues(clickedMarker=NULL) # produce the basic leaflet map with single marker output$map <- renderLeaflet( leaflet() %>% addProviderTiles("CartoDB.Positron") %>% addCircleMarkers(lat = 54.406486, lng = -2.925284) ) # observe the marker click info and print to console when it is changed. observeEvent(input$map_marker_click,{ data$clickedMarker <- input$map_marker_click print(data$clickedMarker)} ) observeEvent(input$map_click,{ data$clickedMarker <- NULL print(data$clickedMarker)}) }) 
+7
source

All Articles