title: Is it possible to access R Leaflet layer controls in Shiny (outside of leaflet)?
body: <p>I'm working on creating a Shiny/Leaflet app similar to <a href="http://public.tableau.com/profile/ifpri.td7290#!/vizhome/2014GHI/2014GHI" rel="noreferrer">this one that's done in tableau</a>. It shows world-level views of poverty for different years, allowing the user to filter the map by variable, region, and year. </p>
  
  <p>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. </p>
  
  <p>Ideally, I would use Shiny controls to toggle the different map layers and use <code>leafletProxy</code> to update the map. But because each layer change draws the entire map again, this is also quite slow. </p>
  
  <p>When I include the different layers <em>inside</em> Leaflet, the layers are rendered much, much faster. (I assume that this is because the <code>addLayersControl</code> option in Leaflet only changes the <code>fillColor</code> of the polygons rather than redrawing the entire global shapefile, as is done with <code>leafletProxy</code>). But is there any way to access these layers outside of Leaflet? </p>
  
  <p>To illustrate, here's some dummy code: </p>
  
  <pre><code>#load required libraries 
  library(shiny)
  library(leaflet)
  library(raster)
  
  #begin shiny app
  shinyApp(
  
    ui &lt;- fluidPage(
      leafletOutput("map", width = "100%", height = 600) 
    ), #END UI
  
    server &lt;- function(input, output, session){
  
      #load shapefile
      rwa &lt;- getData("GADM", country = "RWA", level = 0)
  
      #render map
      output$map &lt;- renderLeaflet({
        leaflet() %&gt;% 
          addTiles() %&gt;% 
          addPolygons(data = rwa, 
                      fillColor = "blue", 
                      group = "blue") %&gt;% 
          addPolygons(data = rwa, 
                      fillColor = "red", 
                      group = "red") %&gt;% 
          addLayersControl(baseGroups = c("blue", "red"), 
                           options = layersControlOptions(collapsed = F))
      }) #END RENDER LEAFLET 
    } #END SERVER
  ) #END SHINY APP
  </code></pre>
  
  <p>Which has the following output:
  <a href="https://i.stack.imgur.com/a4Yk5.png" rel="noreferrer"><img src="https://i.stack.imgur.com/a4Yk5.png" alt="enter image description here"></a></p>
  
  <p>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 <code>observeEvent</code>. Is this possible/how can I do this?</p>