如何从selectInput中的列表中设置View

时间:2017-01-22 23:23:57

标签: r shiny leaflet

我正在尝试从列表中选择一个地方,然后转到传单包提供的地图。

我试过了:

首先,在ui上创建变量

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

然后设置面板

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)
                  ),
)

在服务器中,在相应位置添加标记

  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) 
  })

1 个答案:

答案 0 :(得分:3)

使用data.frame存储每个位置的纬度/经度,然后使用observeEvent并在选择更改时更新传单。

要更新闪亮的传单,您应该使用leafletProxy更新地图

这是一个有效的例子

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)