Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to access R Leaflet layer controls in Shiny (outside of leaflet)?

Tags:

r

leaflet

shiny

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: enter image description here

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?

like image 373
Lauren Avatar asked Jan 04 '17 16:01

Lauren


People also ask

What are layers in Leaflet?

In Leaflet, a “layer” is anything that moves around when the map is moved around. Before seeing how to create them from scratch, it's easier to explain how to do simple extensions.

How do you add a layer to a map in Leaflet?

Step 1 − Create a Map object by passing a <div> element (String or object) and map options (optional). Step 2 − Create a Layer object by passing the URL of the desired tile. Step 3 − Add the layer object to the map using the addLayer() method of the Map class.


1 Answers

You can define an observer for the {MAP_ID}_groups input in your Shiny server.

Example:

server <- function(input, output, session) {
    # ...

    output$my_map <- renderLeaflet({
        # ...
    })

    observe({
        selected_groups <- req(input$my_map_groups)
        # do whatever ... 
    })
}

This input gets updated when the user selects a group in the layers control.

like image 154
Simon G. Avatar answered Oct 05 '22 14:10

Simon G.