单张地图未在Shiny中呈现

时间:2019-04-05 12:14:09

标签: shinydashboard shiny-reactivity r-leaflet selectinput

我在设置带有传单的地图时遇到麻烦。用户从三个下拉菜单中选择后,如何获取地图以显示更新的数据;传单地图不会基于三个下拉列表更新点,这三个下拉列表基本上是在单个数据框中过滤列。

我正在尝试使用SelectInputs重新创建此repo。我的原始数据框大约有494000条记录,格式很长。

这是一个可复制的示例:

gdp = read.xlsx("mexico2.xlsx", header= T) %>%
  as.data.frame()

mexico <- readOGR("mexico.shp", layer = "mexico", encoding = "UTF-8")

mexico_shape@data <- left_join(gdp, mexico@data, by = "census")

#UI.R

ui <- dashboardPage(skin = "blue",
  dashboardHeader(title = "Mexico Data",
                  titleWidth = 350),

  dashboardBody(
    tabItems(
       tabItem ("map1", 
              column(9,
                   leafletOutput("mex_map", height = "600px", width = "100%") 
              ),
              column(width = 3,
                     box(
                       width= NULL,
                       selectInput('indicator', 'Select an indicator', 
                                   choices = unique(mexico_shape@data$variable)),
                                   selected = mexico_shape@data$variable[1]),
                       selectInput("year", 'Select year', 
                                   choices = unique(mexico_shape@data$Year),
                                   selected = mexico_shape@data$Year[25]),
                       selectInput("vote", "By vote share", choices = unique(mexico_shape@data$`leading party`)))),

      tabItem("data1", 
                  dataTableOutput("table_mexico")
              )
    )))  

server.R

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


subsetData <- reactive({
  df <- mexico_shape@data %>% 
    filter(variable == input$indicator) %>% 
    filter(Year == input$year) %>% 
    filter(`leading party` == input$vote)
  return(df)
})

lat = 18.5937
lng = 80.9629
zoom = 4

output$mex_map <- renderLeaflet({

 subsetData() %>% leaflet() %>% 
    addProviderTiles("CartoDB.Positron") %>% 
    addPolygons(data = mexico_shape, fillColor = ~qpal(value), fillOpacity = 0.6, color = "white", weight = 2)
    setView(lat = lat, lng = lng, zoom = zoom)

})


  observe({

   qpal <- colorQuantile("YlGn", mexico_shape@data$value, n = 5, na.color = "#bdbdbd")

    leafletProxy("mex_map", data = subsetData()) %>% 
      clearShapes() %>% 
     addPolygons(data = mexico_shape, fillColor = ~qpal(value), fillOpacity = 0.6, color = "white", weight = 2)

})


  }


# Run the application 
shinyApp(ui = ui, server = server)

我的地图加载了基本的传单调用,但在观察后开始挂起,并调用了传单代理。有办法解决吗?

0 个答案:

没有答案