R:“有光泽和单张”地图:拾取要渲染在地图中的变量后,带有滑块的子集(多边形)

时间:2019-03-22 15:26:24

标签: r shiny slider leaflet

我有一个CSV和shapefile,其中包含地区和许多社会人口统计变量(收入,人口,平方米价格和犯罪率)。我可以合并它们,还可以选择要在传单地图中显示的变量,并带有一个下拉列表。色彩填充越强,值越高。这部分很容易。到目前为止,下面的简化代码是可行的:

library(shiny)
library(shinydashboard)
library(leaflet)
library(rgdal)


#### LOAD DATA ####
CSVDataFrame<- read.csv("CSVDataFrame.csv")
shapeData <- readOGR("districts.shp")

CSVDataFrame <- merge(CSVDataFrame,shapeData, by=c("id"))

################ FUNCTION ################

DataType <- function(x) {
  switch(x,

         "Population" = CSVDataFrame$Population,
         "Income" = CSVDataFrame$Income,
         "Square Meter Price" = CSVDataFrame$SQMP,
         "Crime Rate" = CSVDataFrame$CrimeRate,     
  )
}

################ UI ################

ui <- dashboardPage(
  dashboardHeader(title = "Districts"),
  dashboardSidebar(
    sidebarMenu(

               selectInput("pType", "",
                            list("Population",
                                 "Income",
                                 "Square Meter Price",
                                 "Crime Rate",
                                      ))

      )),

  dashboardBody(
    tabBox(
      width = 12, title = tagList(shiny::icon("map"), "Created by me"),
      tabPanel("Districts viewer",
               tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
               leafletOutput("map",height = "75vh"))
    )
  )
)

################ SERVER ################

server <- function(input, output) {

  output$map <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$CartoDB.Positron,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%
      setView(lng = -58.450034, lat = -34.599585, zoom = 12) %>%
      addPolygons(data= shapeData,weight=0,col = "Red", popup=paste(DataType(input$pType), sep=" <br>") ,
                  fillOpacity = (DataType(input$pType))/max(DataType(input$pType)))
  })

  leafletOutput('map', height=2000)

}

shinyApp(ui, server)

这很好用。现在,我需要添加一个滑块(sliderInput),该滑块能够将数据从MIN输入扩展为MAX输入。我尝试了一些代码,但我不知道该怎么做:

1)在下拉列表中选择变量之后,使sliderInput中的MIN和MAX值更新(我可以使sliderinput对一个静态变量采用MIN和MAX,但这不是我所需要的)

2)将滑块子集设为我当前看到的变量。

非常感谢您的帮助!谢谢

0 个答案:

没有答案