R - 当地图嵌入 Shiny 应用程序并通过观察者呈现时,Leaflet 不显示自定义集群标记
R - Leaflet doesn't display custom Cluster Markers when map is embed in a Shiny app and rendered through observer
在 Shiny 中,我制作了一个带有集群标记的传单地图,集群图标是自定义的,由我通过 JavaScript(JS()
函数)定义。 这样的映射应该是反应式的,基于 radioButton()
中的用户输入。因此,我使用带有 if 语句的 observer()
,通过 leafletProxy()
.
更新地图
当簇的图标是自定义的,而不是Leaflet的默认图标时,标记簇甚至不会出现。 leafletProxy()
似乎无法处理 observer()
的“反应性”。下面是一个最小的可重现示例。
library(shiny)
library(leaflet)
ui <- fluidPage(
br(),
radioButtons(inputId = "markers",
label = NULL,
choices = c("Type A",
"Type B"),
selected = "Type A"),
br(),
leafletOutput(outputId = "map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = 178.441895,
lat = -18.141600,
zoom = 3.5)
})
observe({
if (input$markers == "Type A") {
leafletProxy(mapId = "map") %>%
clearMarkerClusters() %>%
addMarkers(data = quakes,
lng = ~long,
lat = ~lat,
clusterOptions = markerClusterOptions())
} else {
leafletProxy(mapId = "map") %>%
clearMarkerClusters() %>%
addMarkers(data = quakes,
lng = ~long,
lat = ~lat,
clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {
return new L.Icon({iconUrl: 'https://nicocriscuolo.github.io/resistancebank_plots/markers/marker_3D.png',
iconSize: [18, 27],
iconAnchor: [10, 26],
shadowUrl: 'https://nicocriscuolo.github.io/resistancebank_plots/markers/shadow-marker_3D.png',
shadowSize: [26, 39],
shadowAnchor: [5, 28]});}")))
}
})
}
shinyApp(ui, server)
可能的解决方案是什么?
这个问题终于被Joe Cheng解决了。此处的解决方案:
https://github.com/rstudio/leaflet/pull/696
确保通过以下方式安装更新的 leaflet 版本:
remotes::install_github("rstudio/leaflet")
在 Shiny 中,我制作了一个带有集群标记的传单地图,集群图标是自定义的,由我通过 JavaScript(JS()
函数)定义。 这样的映射应该是反应式的,基于 radioButton()
中的用户输入。因此,我使用带有 if 语句的 observer()
,通过 leafletProxy()
.
当簇的图标是自定义的,而不是Leaflet的默认图标时,标记簇甚至不会出现。 leafletProxy()
似乎无法处理 observer()
的“反应性”。下面是一个最小的可重现示例。
library(shiny)
library(leaflet)
ui <- fluidPage(
br(),
radioButtons(inputId = "markers",
label = NULL,
choices = c("Type A",
"Type B"),
selected = "Type A"),
br(),
leafletOutput(outputId = "map")
)
server <- function(input, output, session) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles() %>%
setView(lng = 178.441895,
lat = -18.141600,
zoom = 3.5)
})
observe({
if (input$markers == "Type A") {
leafletProxy(mapId = "map") %>%
clearMarkerClusters() %>%
addMarkers(data = quakes,
lng = ~long,
lat = ~lat,
clusterOptions = markerClusterOptions())
} else {
leafletProxy(mapId = "map") %>%
clearMarkerClusters() %>%
addMarkers(data = quakes,
lng = ~long,
lat = ~lat,
clusterOptions = markerClusterOptions(iconCreateFunction = JS("function (cluster) {
return new L.Icon({iconUrl: 'https://nicocriscuolo.github.io/resistancebank_plots/markers/marker_3D.png',
iconSize: [18, 27],
iconAnchor: [10, 26],
shadowUrl: 'https://nicocriscuolo.github.io/resistancebank_plots/markers/shadow-marker_3D.png',
shadowSize: [26, 39],
shadowAnchor: [5, 28]});}")))
}
})
}
shinyApp(ui, server)
可能的解决方案是什么?
这个问题终于被Joe Cheng解决了。此处的解决方案:
https://github.com/rstudio/leaflet/pull/696
确保通过以下方式安装更新的 leaflet 版本:
remotes::install_github("rstudio/leaflet")