I'm working on creating a Shiny/Leaflet app similar to this one that's done in tableau. It shows world-level views of poverty for different years, allowing the user to filter the map by variable, region, and year.
The problem is that the global country-level shapefile (from NaturalEarthData) renders quite slowly. I'm working on different ways to simplify those polygons to decrease load time, but in the meantime, I'm working on other potential solutions.
Ideally, I would use Shiny controls to toggle the different map layers and use leafletProxy
to update the map. But because each layer change draws the entire map again, this is also quite slow.
When I include the different layers inside Leaflet, the layers are rendered much, much faster. (I assume that this is because the addLayersControl
option in Leaflet only changes the fillColor
of the polygons rather than redrawing the entire global shapefile, as is done with leafletProxy
). But is there any way to access these layers outside of Leaflet?
To illustrate, here's some dummy code:
#load required libraries
library(shiny)
library(leaflet)
library(raster)
#begin shiny app
shinyApp(
ui <- fluidPage(
leafletOutput("map", width = "100%", height = 600)
), #END UI
server <- function(input, output, session){
#load shapefile
rwa <- getData("GADM", country = "RWA", level = 0)
#render map
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addPolygons(data = rwa,
fillColor = "blue",
group = "blue") %>%
addPolygons(data = rwa,
fillColor = "red",
group = "red") %>%
addLayersControl(baseGroups = c("blue", "red"),
options = layersControlOptions(collapsed = F))
}) #END RENDER LEAFLET
} #END SERVER
) #END SHINY APP
Which has the following output:
You can easily toggle between the blue and red layers within the leaflet map object. But let's say that I want a Shiny table to update with the attributes from the red polygon layer when I toggle the map layers from blue to red. I want to be able to pull this object outside of leaflet and utilize it in a Shiny observeEvent
. Is this possible/how can I do this?
You can define an observer for the
{MAP_ID}_groups
input in your Shiny server.Example:
This input gets updated when the user selects a group in the layers control.