Search code examples
rshinyr-leaflet

Update multiple selectInput widget based on Leaflet marker click


I have a shiny application that uses a multiple inputSelection widget used to update a plot based on user input.

selectInput("selectedSites", "Select Site(s) :",
                sensor_locations$description, multiple = TRUE,
                selected = sensor_locations$description)

I now also want to allow the user to select sites (choices allowed through the inputSelection widget) by clicking on markers placed on a leaflet map.

I have done this as follows: By observing clicks on the markers and updating the inputSelection box.

observeEvent(input$sitemap_marker_click, {
    click <- input$sitemap_marker_click
    print(click)
    print(click$id)
    updateSelectInput(session, "selectedSites", 
                      selected = click$id)
  })

This works fine, except that it only accommodates for a single input. The previous selections on the inputSelect widget are removed and is updated by the site represented by the marker click.

I however want to add onto the existing selection and not replace it.


Solution

  • Managed to resolve the issue partially by including a combination of the previously selected values and clicked on marker value.

    observeEvent(input$sitemap_marker_click, {
        click <- input$sitemap_marker_click
        updateSelectInput(session, "selectedSites", 
                          selected = c(input$selectedSites, click$id))
      })
    

    I'd however like to also remove the selected marker value from the list upon clicking it, if it already exists.