I was trying to create an interactive map that allows the user to select the states that they want to look for by subsetting the shapefile from census.gov with the following UI function and server function:
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
})
However, the output does not show any map on the display window while the other parts run correctly with no error but some warnings: 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