Integrate Leaflet Deflate with Shiny

145 views Asked by At

Updated

After finding this... I adjusted the question a bit. I have successfully loaded and used L.deflate to create my "deflated features". But now I need to somehow take my objects from the map and add them to the deflate object as described in the leaflet.deflate documenation.


I am trying to use the steps here to integrate the leaflet deflate plugin into a Shiny application. I've loaded up the plugin, but now I just want to know how to take the objects from my Polygon layer and add them to the delate_features object as described in the Documentation.

Documentation Steps

const map = L.map("map").setView([51.505, -0.09], 12);

const deflate_features = L.deflate({minSize: 20});
deflate_features.addTo(map);

// This is the step I am not sure how to handle in the onRender()
const polygon = L.polygon([
    [51.509, -0.08],
    [51.503, -0.06],
    [51.51, -0.047]
]);
polygon.addTo(deflate_features);

Test App

library(sf)
library(leaflet)
library(htmltools)
library(htmlwidgets)

js_file <- "https://unpkg.com/[email protected]/dist/L.Deflate.js"


ui <- fixedPage(
  tags$head(tags$script(src = js_file)),
  leafletOutput("map")
)

server <- function(session, input, output) {
  
  output$map <- renderLeaflet({
    nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
    
    leaflet(options = leafletOptions(preferCanvas = TRUE)) %>% 
      addMapPane(name = "labels", zIndex = 650) %>% 
      addProviderTiles(providers$Stamen.TonerLabels,
                       options = providerTileOptions(opacity = 0.35,
                                                     updateWhenZooming = FALSE,
                                                     pane = "labels")) %>% 
      addProviderTiles(providers$Stamen.TonerBackground,
                       options = providerTileOptions(opacity = 0.35,
                                                     updateWhenZooming = FALSE)) %>% 
      addPolygons(data = st_transform(nc, 4326)) %>% 
      onRender("function(el, x, data) {
               const deflate_features = L.deflate({ minSize: 20 });
               deflate_features.addTo(this);
               // How to select the polygons I already have and add them                       
               // to deflate_features????
      }")
    
  })
}
  
shinyApp(ui, server)


0

There are 0 answers