在Leaflet的反应式子集中为Shiny调谐器绘制SpatialPolygonsDataFrame时,SPDF为空时会出错

时间:2018-12-17 14:35:53

标签: r shiny leaflet

因此,我已经使用美国所有县的geoJSON数据创建并运行了一张闪亮的地图。我对每个县都有一些附加的指标,因此我基本上在使用SpatialPolygonsDataFrames。该地图当前需要一些输入(平均体积等)并过滤geoJSON数据,因此该地图仅呈现通过过滤器的县。我试图弄清楚如何处理过滤器最终删除所有县多边形的情况(即所有县都没有通过过滤器)。现在,地图仅在发生这种情况时崩溃并返回此错误:

  

在polygonData.SpatialPolygonsDataFrame(数据)中的警告:空   SpatialPolygonsDataFrame对象已传递,将被跳过

     

警告:总和错误:参数的无效“类型”(列表)[无堆栈   跟踪可用]

代码的相关部分在这里:HttpContext

global.R

data_sets <- list(countyborder2006, countyborder2007, countyborder2008, countyborder2009, countyborder2010, countyborder2011, countyborder2012, countyborder2013, countyborder2014, countyborder2015, countyborder2016, countyborder2017, countyborder2018, countyborder_all)

ui.R

conditionalPanel("input.level == 'County level'", selectInput("year", "Year:", choices = c("2006","2007","2008","2009","2010","2011", "2012", "2013","2014","2015","2016","2017","2018", "All years" = "2019"), selected = "2019" ), numericInput("opcrange", label = "Minimum ops vol:", min = 0, max = 10000000, value = 0 ), numericInput("opppcrange", label = "Minimum ops ppa:", min = 0, max = 150, value = 0 ) ), numericInput("oppcrange", label = "Minimum % of ops:", min = -1, max = 1, value = -1 ), numericInput("ohpcrange", label = "Minimum % of others:", min = -1, max = 1, value = -1) )

server.R

我尝试使用条件语句不使用# filter data according to parameters set for customer level filteredData <- reactive({ req(input$opcrange) req(input$opppcrange) req(input$oppcrange) req(input$ohpcrange) else if (input$level == "County level") { countyborder <- data_sets[[(as.numeric(input$year) - 2005)]] if (input$oporoh == "Opioids") { countyborder[countyborder@data$avg_opioid >= input$opcrange & countyborder@data$avg_oxy_hydro >= input$ohcrange & countyborder@data$avg_opioid_ppp >= input$opppcrange & countyborder@data$avg_opioid_perc >= input$oppcrange & countyborder@data$avg_oxy_hydro_perc >= input$ohpcrange,] } else { countyborder[countyborder@data$avg_opioid >= input$opcrange & countyborder@data$avg_oxy_hydro >= input$ohcrange & countyborder@data$avg_oxy_hydro_ppp >= input$ohppcrange & countyborder@data$avg_opioid_perc >= input$oppcrange & countyborder@data$avg_oxy_hydro_perc >= input$ohpcrange,] } } }) # render base map that isn't redrawn every time output$map <- renderLeaflet({ leaflet() %>% addProviderTiles("CartoDB.Positron", options = providerTileOptions(noWrap = TRUE)) %>% #Add default OpenStreetMap map tiles setView(-99, 45, zoom = 4) %>% #set view over US addScaleBar(position = "topleft") %>% addMeasure(position = "topleft") }) # this observer controls all the markers for customer level info observe({ else if (input$level == "County level") { withProgress(message = "Rendering...", value = 0.1, { pal <- colorBin("YlOrRd", bins = c(0, 1, 2, 3, 4, 5, 6, 10, 20, Inf), filteredData()$avg_ops_ppp,pretty = FALSE) leafletProxy("map", data = filteredData()) %>% clearMarkers() %>% clearMarkerClusters() %>% clearShapes() %>% addPolygons( stroke = TRUE, color = "white", highlight = highlightOptions( weight = 2, fillOpacity = 0.6, color = "#666", opacity = 0.8, bringToFront = TRUE, sendToBack = TRUE ), opacity = 1, weight = 0.5, smoothFactor = 0.2, fillOpacity = 0.8, fillColor = pal(filteredData()$avg_ops_ppp), label = lapply(countyInfo, HTML) ) %>% clearControls() %>% addLegend( "bottomleft", pal = pal, values = filteredData()$avg_ops_ppp, title = "Ops ppa per month", layerId = "countyLegend" ) }) } }) 映射任何内容,但这似乎也不起作用。还有其他解决方法吗?不幸的是,我无法共享数据,但是县形状文件来自http://eric.clst.org/tech/usgeojson/

2 个答案:

答案 0 :(得分:0)

您可以添加

req(filteredData()) 

req(filteredData()@data)

req(length(filteredData()@data) != 0)

req(nrow(filteredData()@data) != 0)

(取决于您的反应性数据集),在最后一个观察者的开头,它将多边形添加到传单地图中。

如果没有数据可绘制,这将停止执行。

或者根据req()文档:

  

如果任何给定值不正确,操作将停止   提出“无声”例外

答案 1 :(得分:0)

我设法使用如下条件来解决它:

 observe({
    else if (input$level == "County level") {
      withProgress(message = "Rendering...", value = 0.1, {
       if (is.data.frame(filteredData()@data) & nrow(filteredData()@data) == 0){
        leafletProxy("map") %>%
          clearMarkers() %>%
          clearMarkerClusters() %>%
          clearShapes()
         } else {
          pal <- colorBin("YlOrRd", bins = c(0, 1, 2, 3, 4, 5, 6, 10, 20, Inf), filteredData()$avg_ops_ppp,pretty = FALSE)

          leafletProxy("map", data = filteredData()) %>%
            clearMarkers() %>%
            clearMarkerClusters() %>%
            clearShapes() %>%
            addPolygons(
              stroke = TRUE,
              color = "white",
              highlight = highlightOptions(
                weight = 2,
                fillOpacity = 0.6,
                color = "#666",
                opacity = 0.8,
                bringToFront = TRUE,
                sendToBack = TRUE
              ),
              opacity = 1,
              weight = 0.5,
              smoothFactor = 0.2,
              fillOpacity = 0.8,
              fillColor = pal(filteredData()$avg_ops_ppp),
              label = lapply(countyInfo, HTML)
            ) %>%
            clearControls() %>%
            addLegend(
              "bottomleft",
              pal = pal,
              values = filteredData()$avg_ops_ppp,
              title = "Ops ppa per month",
              layerId = "countyLegend"
            )
         }
      })
    }
  })