I created the same type of application that worked out the G. Cocca code, and after a few months I messed with it again and again, I came up with a more elegant solution to your problem. For easy reproducibility, I use Rwanda shapefiles as an example (because they are much smaller than USA GADM shapefiles, but you can always replace them 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 (ie 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 conclusion is a map of your state level. Using this code, when you click on a state of interest, a click object is created that has click$id corresponding to the name of this state (which is set in the definition of layerId in the addPolygons call). With click$id as the selected state name, you can multiply your county county polygon with this state and plot it in the form of a map.
The design options for this card are actually endless. Hope this helps!