如何在Shiny中整合使用Leaflet制作的多个等值线图?

时间:2017-07-20 15:46:13

标签: r shiny leaflet choropleth

我在Leaflet中创建了三个单独的等值区域地图,所有这些地图都在整个欧洲,并且使用了相同的国家/地区。每个等值区域地图显示不同类型的比例,并且在1999年和2009年有两个层次。

虽然我已成功将choropleth地图与年份图层分别整合到Shiny中,但我希望能够只使用一个带有下拉栏的Shiny地图在三个不同的等值区域地图之间点击,同时仍保留我的图层每张地图。

有人可以建议怎么做吗?

1 个答案:

答案 0 :(得分:0)

您可以使用leafletProxy。我稍微修改了here中的示例,以更好地满足您的要求。希望这有帮助!

library(shiny)
library(leaflet)

ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10,
                selectInput("maptype", "Map type: ",
                            choices = c("map 1", "map 2", "map 3")
                )
  )
)

server <- function(input, output, session) {

  # Reactive expression for the data subsetted to what the user selected
  filteredData <- reactive({
    if(input$maptype=="map 1") return(quakes[sample(1:nrow(quakes),50,replace=TRUE),])
    if(input$maptype=="map 2") return(head(quakes,50))
    if(input$maptype=="map 3") return(tail(quakes,50))

  })


  output$map <- renderLeaflet({
    leaflet(quakes) %>% addTiles() %>%
      fitBounds(~min(long), ~min(lat), ~max(long), ~max(lat))
  })

  observe({
    leafletProxy("map", data = filteredData()) %>%
      clearShapes() %>%
      addCircles(radius = ~10^mag/10, weight = 1, color = "#777777", fillOpacity = 0.7, popup = ~paste(mag)
      )
  })


}

shinyApp(ui, server)