leaflet.esri
leaflet.esri copied to clipboard
FeatureLayer with dynamic style doesn't work via leafletProxy()
Passing JS()
in featureLayerOptions()
doesn't work, but passing JSON (via list()
) or interacting with the leaflet()
(instead of the leafletProxy()
) works.
The following minimalistic example app shows the problem. Switch between these modes by changing the value of demo
in the first line. Shiny is required to replicate.
# Example works with "map" and "json", doesn't work with "js"
demo <- "js" # "map", "json", "js"
library(shiny)
library(leaflet)
library(leaflet.esri)
# Define UI for application that draws a histogram
ui <- fillPage(
leafletOutput("map")
)
# Define server logic required to draw a histogram
server <- function(input, output, session) {
output$map <-
renderLeaflet({
map <-
leaflet() %>%
addEsriBasemapLayer(esriBasemapLayers$Gray) %>%
setView(-118.22, 33.836, 8)
if (demo == "map") {
map <-
map %>%
addEsriFeatureLayer(
layerId = "Map object",
url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Congressional_Districts/FeatureServer/0",
fillOpacity = 0.5,
options = featureLayerOptions(
simplifyFactor = 0.5, precision = 5,
style = JS('{ fillColor: "green" }')))
}
map
})
if (demo == "json") {
onFlushed(function() server_json(input, output, session))
} else if (demo == "js") {
onFlushed(function() server_js(input, output, session))
} else if (demo != "map") {
stop()
}
}
server_json <- function(input, output, session) {
leafletProxy("map") %>%
addEsriFeatureLayer(
layerId = "Proxy JSON",
url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Congressional_Districts/FeatureServer/0",
fillOpacity = 0.5,
options = featureLayerOptions(
simplifyFactor = 0.5, precision = 5,
style = list(fillColor = "green")
)
)
}
server_js <- function(input, output, session) {
leafletProxy("map") %>%
addEsriFeatureLayer(
layerId = "Proxy JS",
url = "https://services.arcgis.com/P3ePLMYs2RVChkJx/arcgis/rest/services/USA_Congressional_Districts/FeatureServer/0",
fillOpacity = 0.5,
options = featureLayerOptions(
simplifyFactor = 0.5, precision = 5,
style = JS('{ fillColor: "green" }')
)
)
}
# Run the application
shinyApp(ui = ui, server = server)