I've created the same type of app working off of G. Cocca's code, and after a few months of fiddling with it over and over, I've come up with a more elegant solution to your problem. For simple reproducibility, I'm using Rwanda shapefiles as an example (because they're much smaller than GADM's USA shapefiles, but you can always just replace these with your own US shapefiles).
library(raster)
library(shiny)
library(leaflet)
library(RColorBrewer)
#load in shapefiles for state and county level
states <- getData("GADM", country = "rwa", level = 1)
counties <- getData("GADM", country = "rwa", level = 2)
#define color palettes for states
pal <- brewer.pal(8, "Dark2")
statePal <- colorFactor(pal, states@data$NAME_1)
shinyApp(
ui = fluidPage(
leafletOutput('myMap', width = "100%"),
br(),
leafletOutput("myMap2", width = "100%")
), #END UI
server <- function(input, output, session){
#default state level map output
output$myMap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = states,
fillColor = ~statePal(states@data$NAME_1),
fillOpacity = 1,
color = "white",
stroke = T,
weight = 1,
layerId = states@data$NAME_1) #this sets the click id, very important!
}) #END RENDERLEAFLET OUTPUT
observeEvent(input$myMap_shape_click, {
#define click object
click <- input$myMap_shape_click
#subset counties shapefile so that only counties from the clicked state are mapped
selected <- counties[counties$NAME_1 == click$id,]
#define color palette for counties
countyPal <- colorFactor(pal, selected@data$NAME_2)
#if click id isn't null (i.e. if ANY polygon is clicked on), draw map of counties
if(!is.null(click$id)){
output$myMap2 <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = selected,
fillColor = ~countyPal(selected@data$NAME_2),
fillOpacity = 1,
color = "white",
stroke = T,
weight = 1)
}) #END RENDERLEAFLET
} #END CONDITIONAL
}) #END OBSERVE EVENT
}) #END SHINYAPP
The first output is your state level map. With this code, when you click on a state of interest, a click object is created that has a click$id
corresponding the name of that state (which is established in the layerId
definition in the addPolygons
call). With the click$id
as the selected state name, you can then subset your county level polygon by that state and plot it as a map.
The options for designing this map are endless, really. Hope this helps!