因此,我使用了美国所有县的 geoJSON 数据制作并运行了一张闪亮的地图。我对每个县都有一些附加指标,因此我本质上是在使用 SpatialPolygonsDataFrames。该地图当前接受一些输入(平均体积等)并过滤 geoJSON 数据,以便地图仅呈现通过过滤器的县。我试图弄清楚如何处理过滤器最终删除所有县多边形的情况(即没有一个县通过过滤器)。现在,当发生这种情况时,地图就会崩溃并返回此错误:
polygonData.SpatialPolygonsDataFrame(data) 中的警告:空 SpatialPolygonsDataFrame 对象已传递并将被跳过
警告:总和错误:参数的“类型”(列表)无效[无堆栈 跟踪可用]
代码的相关部分在这里:
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"
)
})
}
})
我尝试使用条件来不使用
if (is.data.frame(countyborder@data) & nrow(countyborder@data) == 0) {}
映射任何内容,但这似乎也不起作用。还有其他解决方法吗?不幸的是,我无法共享数据,但县形状文件来自 http://eric.clst.org/tech/usgeojson/。
我设法使用这样的条件来解决它:
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"
)
}
})
}
})
您可以添加
req(filteredData())
或
req(filteredData()@data)
或
req(length(filteredData()@data) != 0)
或
req(nrow(filteredData()@data) != 0)
(取决于您的反应数据集),在最后一个观察者的开头,它将多边形添加到传单地图中。
如果没有数据可供绘制,这将停止执行。
或者根据
req()
文档:
如果任何给定值不为真,则操作将停止 引发“无声”异常