将selectInput值传递给map

时间:2016-07-16 21:46:13

标签: r shiny gis

我试图弄清楚如何在UI端使用通过selectInput选择的变量来填充服务器端的地图数据。虽然我已经成功手动设置变量以通过filteredData显示,但它对于可以动态更改的输入无法正常工作。是否有一些功能/解决方案可以帮助根据用户输入将数据子集显示在地图上?

UI.R

districts <- readOGR("location",layer)
metadata <- read.csv("metadata.csv")

#Prepare list of values for the selectInput boxes
COMMUNITYSAFETY<-as.list(metadata$variable[metadata$group == "Community Safety"])
names(COMMUNITYSAFETY)=metadata$description[metadata$group == "Community Safety"]

##CONTINUED FOR REST OF SELECTINPUT BOXES

ui <- fluidPage(
  titlePanel("Map"),
  sidebarPanel(
    selectInput("vr", "Would you like to show the variable by value or rank?",
                c("Value" = "_v",
                  "Rank" = "_r")
    ),
    selectInput("group", "Select Group:",
                c("Community Safety" = "COMMUNITYSAFETY",
                  "Demographics" = "DEMOGRAPHICS",
                  "Healthy People and Environments" = "HEALTHY_PEOPLE_ENVM",
                  "Housing" = "HOUSING",
                  "Income and Employment" = "INCOME_EMPL",
                  "Lifelong Learning" = "LIFE_LEARNING",
                  "Transportation" = "TRANSPORTATION")),
    #if user choses Community Safety
    conditionalPanel(
      condition = "input.group == 'COMMUNITYSAFETY'",
      selectInput("var", "Community Safety Variables:",
                  COMMUNITYSAFETY)),

    #repeat other conditional panels for inputs

  mainPanel(
    tabsetPanel(  
      tabPanel("Map", leafletOutput("map", height=800)),
      tabPanel("Histogram")
))))

Server.R:

server <- function(input, output, session){
  filteredData <- reactive({
    subset(districts,select=input$var)
  })

...

  observe({
    leafletProxy("map", data=districts) %>%
      addPolygons(stroke=TRUE,
                  fillColor=pal(filteredData()),
                  color="black",
                  weight=.5)
  }) 
}

2 个答案:

答案 0 :(得分:0)

我认为问题是,你如何使用leafletProxy。输入?leafletProxy,您会看到,leafletProxy没有任何data参数!

  

leafletProxy(mapId,session = shiny :: getDefaultReactiveDomain(),   data = NULL,deferUntilFlush = TRUE)

因此,您需要在代码的第二部分中添加data districts,例如:

addPolygons(data = districts, stroke=TRUE,
    fillColor=pal(filteredData()),
    color="black", weight=.5)

您应该记住的是districts应该是leaflet理解的内容。 leaflet可以在sp包的objects中显示spatial class的帮助page says。因此,您必须将您的区域转换为SpatialPolygon

假设您的矩阵包含lat lng列,其中每个条目都是您多边形的节点/点之一,此函数会将矩阵转换为所需的输出,以便在地图上显示。

makePoly&lt; - function(myMatrix){

if (nrow(myMatrix) >= 3) {
    # create Polygon
    myPolygon <- Polygon(myMatrix)

    # create Polygon List
    myPolygonList <- Polygons(list(myPolygon), 1)

    # create spatial Polygon 
    # why? -> can have a CRS associated with it!
    myPolygonSpatial <- SpatialPolygons(list(myPolygonList))

    # assign CRS
    proj4string(myPolygonSpatial) <- CRS("+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs")

    return(myPolygonSpatial)

} else {
    print("Matrix to short")
}

答案 1 :(得分:0)

我认为问题非常简单,您可以简单地使用subset()函数对数据进行子集化并使用数据进行绘图。

请考虑以下代码:

districts <- readOGR("location",layer)
output$Map = renderLeaflet({
filterData = subset(districts, districts$variable = input$var)
mapStates = map("world",region= filterData$variable, fill = TRUE, plot = FALSE)
mapa <- leaflet(mapStates) %>% addTiles()%>%
addPolygons(stroke = FALSE, smoothFactor = 0.2, fillOpacity = 0.5,color = "black") %>%
setView(lng = 31, lat = 35, zoom = 1)
})  

此外,您可以使用不同地区/国家/地区的名称替换“世界”而非“世界”。

希望这会有所帮助:)