0

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
Kaihua Hou
  • 89
  • 1
  • 9

1 Answers1

0

It doens't seem like the probelm really has anything to do with leaflet. The problem seems to be in these lines

neStates <- paste(input$statesInput, collapse = ", ");
statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% c(neStates));

When you paste0(collapse=) values, you turn a vector of values into a single stinrg. For example

states <- c("AL", "MO")
paste0(states, collapse=",")
[1] "AL,MO"

However the %in% operator looks for exact matches within a vector, not a comma separated string. See

"AL" %in% "AL,MO"
# [1] FALSE
"AL" %in% c("AL", "MO")
# [1] TRUE

This means you should really just be using

statesAbbr <- subset(statePolygonData, statePolygonData$STUSPS %in% input$statesInput);
MrFlick
  • 195,160
  • 17
  • 277
  • 295