闪亮的网络应用程序中的传单不显示地图
Leaflet in Shiny web application does not display the map
我试图创建一个交互式地图,允许用户通过使用以下 UI 函数从 census.gov 子集 shapefile 来 select 他们想要查找的状态和服务器功能:
library(shiny)
library(leaflet)
library(magrittr)
library(rgdal)
shinyUI(fluidPage(
titlePanel("JHU COVID-19 Modeling Visualization Map"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("statesInput", "Choose the State(s)",
c("AL", "MO", "AK", "MT", "AZ", "NE",
"AR", "NV", "CA", "NH", "CO", "NJ",
"CT", "NM", "DE", "NY", "DC", "NC",
"FL", "ND", "GA", "OH", "HI", "OK",
"ID", "OR", "IL", "PA", "IN", "RI",
"IA", "SC", "KS", "SD", "KY", "TN",
"LA", "TX", "ME", "UT", "MD", "VT",
"MA", "VA", "MI", "WA", "MN", "WV",
"MS", "WI", "WY")),
submitButton("Submit")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Map By State", leafletOutput("statePolygonMap"))
)
)
)))
shinyServer(function(input, output) {
dir <- getwd();
statepolygonZip <- download.file("https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip",
destfile = "cb_2018_us_state_500k.zip");
unzip("cb_2018_us_state_500k.zip");
statePolygonData <- readOGR("cb_2018_us_state_500k.shp", layer = "cb_2018_us_state_500k",
GDAL1_integer64_policy = TRUE);
## obtaning the state shape file data provided by cencus.gov
## for more categories of region shape file:
## https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html
output$statePolygonMap <-renderLeaflet ({
neStates <- paste(input$statesInput, collapse = ", ");
## extracting the all inputs and combining to a string
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% c(neStates));
## subsetting the shape file with the selected states
leaflet(statesAbbr) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE))
})
## producing the map with polygon boundary on the state level
})
但是,输出没有在显示器上显示任何地图 window,而其他部分 运行 正确,没有错误,但有一些警告:
screen shot of the display window
Warning in polygonData.SpatialPolygonsDataFrame(data) :
Empty SpatialPolygonsDataFrame object passed and will be skipped
Warning in polygonData.SpatialPolygonsDataFrame(data) :
Empty SpatialPolygonsDataFrame object passed and will be skipped
看来problem和传单真的没有任何关系。问题似乎出在这几行
neStates <- paste(input$statesInput, collapse = ", ");
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% c(neStates));
当你 paste0(collapse=)
值时,你将一个值向量变成一个单一的字符串。例如
states <- c("AL", "MO")
paste0(states, collapse=",")
[1] "AL,MO"
但是 %in%
运算符在向量中查找精确匹配,而不是逗号分隔的字符串。参见
"AL" %in% "AL,MO"
# [1] FALSE
"AL" %in% c("AL", "MO")
# [1] TRUE
这意味着您真的应该只使用
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% input$statesInput);
我试图创建一个交互式地图,允许用户通过使用以下 UI 函数从 census.gov 子集 shapefile 来 select 他们想要查找的状态和服务器功能:
library(shiny)
library(leaflet)
library(magrittr)
library(rgdal)
shinyUI(fluidPage(
titlePanel("JHU COVID-19 Modeling Visualization Map"),
sidebarLayout(
sidebarPanel(
checkboxGroupInput("statesInput", "Choose the State(s)",
c("AL", "MO", "AK", "MT", "AZ", "NE",
"AR", "NV", "CA", "NH", "CO", "NJ",
"CT", "NM", "DE", "NY", "DC", "NC",
"FL", "ND", "GA", "OH", "HI", "OK",
"ID", "OR", "IL", "PA", "IN", "RI",
"IA", "SC", "KS", "SD", "KY", "TN",
"LA", "TX", "ME", "UT", "MD", "VT",
"MA", "VA", "MI", "WA", "MN", "WV",
"MS", "WI", "WY")),
submitButton("Submit")
),
mainPanel(
tabsetPanel(type = "tabs",
tabPanel("Map By State", leafletOutput("statePolygonMap"))
)
)
)))
shinyServer(function(input, output) {
dir <- getwd();
statepolygonZip <- download.file("https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip",
destfile = "cb_2018_us_state_500k.zip");
unzip("cb_2018_us_state_500k.zip");
statePolygonData <- readOGR("cb_2018_us_state_500k.shp", layer = "cb_2018_us_state_500k",
GDAL1_integer64_policy = TRUE);
## obtaning the state shape file data provided by cencus.gov
## for more categories of region shape file:
## https://www.census.gov/geographies/mapping-files/time-series/geo/carto-boundary-file.html
output$statePolygonMap <-renderLeaflet ({
neStates <- paste(input$statesInput, collapse = ", ");
## extracting the all inputs and combining to a string
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% c(neStates));
## subsetting the shape file with the selected states
leaflet(statesAbbr) %>%
addPolygons(color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE))
})
## producing the map with polygon boundary on the state level
})
但是,输出没有在显示器上显示任何地图 window,而其他部分 运行 正确,没有错误,但有一些警告: screen shot of the display window
Warning in polygonData.SpatialPolygonsDataFrame(data) :
Empty SpatialPolygonsDataFrame object passed and will be skipped
Warning in polygonData.SpatialPolygonsDataFrame(data) :
Empty SpatialPolygonsDataFrame object passed and will be skipped
看来problem和传单真的没有任何关系。问题似乎出在这几行
neStates <- paste(input$statesInput, collapse = ", ");
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% c(neStates));
当你 paste0(collapse=)
值时,你将一个值向量变成一个单一的字符串。例如
states <- c("AL", "MO")
paste0(states, collapse=",")
[1] "AL,MO"
但是 %in%
运算符在向量中查找精确匹配,而不是逗号分隔的字符串。参见
"AL" %in% "AL,MO"
# [1] FALSE
"AL" %in% c("AL", "MO")
# [1] TRUE
这意味着您真的应该只使用
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% input$statesInput);