Hello all,
I was wondering is there a way to click on a marker on a Leaflet map in a shiny app to select an item, thus changing the item already selected in the dropdown menu. I click the station but in the "select a station" button, there is no change (figure below). Here you can find my code, I tried several options. Thank you for your time and help in advance. Here is my code:
library(leaflet)
library(sf)
library(shiny)
geopackage_path <- "C:/Users/yagmur/work/Spain/data/"
year <- 2022
sf_data <- st_read(file.path(geopackage_path, paste0("meteo_andalucia_", year, ".gpkg")))
Select unique locations
unique_locations <- sf_data %>% distinct(station_name, .keep_all = TRUE)
ui <- fluidPage(
fluidRow(
column(width = 4,
selectInput("station", "Select a Station", choices = unique_locations$station_name),
actionButton("compare", "Compare Temperature")
),
column(width = 8,
leafletOutput("map")
)
),
plotOutput("graph")
)
server <- function(input, output, session) {
Initialize the selected station as NULL
selected_station <- reactiveVal(NULL)
Render the leaflet map
output$map <- renderLeaflet({
leaflet(data = unique_locations) %>%
addTiles() %>%
addCircleMarkers(radius = 4,
color = "red",
fillOpacity = 0.8,
label = ~station_name,
group = "markers")
})
Update the selected station when a marker is clicked
observeEvent(input$map_marker_click, {
clicked_marker <- input$map_marker_click
clicked_station <- clicked_marker$label
selected_station(clicked_station)
updateSelectInput(session, "station", selected = clicked_station)
})
}
shinyApp(ui, server)