Search code examples
rshinyr-leaflet

How to setView selecting from a list in selectInput


I'm trying to select a place from a list and then go to it a map provided by the leaflet package.

I tried this:

First, create the variables on the ui

vars <- c(
  "LAS CRUZADAS" = "lc",
  "PUENTE SAN ISIDRO" = "psi",
  "FUNDO EL PROGRESO" = "fep",
  "CALLE SANTA MARÍA" = "csm",
  "ASENTAMIENTO NOGALES" = "an"
)

And then set up the panel

navbarPage("PLATAFORMA NAUTILUS", id="nav",

    absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                  draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                  width = 330, height = "auto",

                  h2(""),


                  selectInput("color", "Seleccionar Estación", vars)
                  ),
)

in the server, add a marker in the respective places

  output$mymap <- renderLeaflet({
    leaflet() %>% addTiles() %>%
                                #h            v
      addCircleMarkers(lng=-71.294563, lat=-32.933843, color="blue" ,popup="LAS CRUZADAS") %>%
      addCircleMarkers(lng=-71.240000, lat=-32.900000, color="blue" ,popup="PUENTE SAN ISIDRO") %>%
      addCircleMarkers(lng=-71.226667, lat=-32.832778, color="blue" ,popup="CALLE SANTA MARÍA") %>%
      addCircleMarkers(lng=-71.183889, lat=-32.733333, color="blue" ,popup="ASENTAMIENTO NOGALES") %>%
      addCircleMarkers(lng=-71.221667, lat=-32.866111, color="blue" ,popup="FUNDO EL PROGRESO") %>%

      setView(lng=-71.294563, lat=-32.933843, zoom=11) 
  })

Solution

  • Use a data.frame to store the lat/lon for each location, then use an observeEvent and to update leaflet when the selection changes.

    To update leaflet in shiny you should use leafletProxy to update the map

    Here's a working example

    library(shiny)
    library(leaflet)
    
    df_vars <- data.frame(location = c("LAS CRUZADAS","ASENTAMIENTO NOGALES"),
                                                lat = c(-32.9338, -32.8661),
                                                lon = c(-71.2945, -71.2216)
    )
    
    ui <- fluidPage(
    
        selectInput(inputId = "myLocations", label = "Locations",
                                choices = df_vars$location),
    
        leafletOutput(outputId = "mymap")
    
    )
    
    server <- function(input, output){
    
        output$mymap <- renderLeaflet({
    
            leaflet() %>% 
                addTiles() %>%
                addCircleMarkers(lng=-71.294563, lat=-32.933843, color="blue" ,popup="LAS CRUZADAS") %>%
                addCircleMarkers(lng=-71.221667, lat=-32.866111, color="blue" ,popup="FUNDO EL PROGRESO")
        })
    
        observeEvent({
            input$myLocations
            },{
                selectedLocation <- df_vars[df_vars$location == input$myLocations, c("lat","lon")]
    
                leafletProxy(mapId = "mymap") %>%
                    setView(lng = selectedLocation$lon, lat = selectedLocation$lat, zoom = 11)
            })
    
    }
    
    shinyApp(ui, server)