在Shiny中,我制作了一张带有聚类标记的传单地图,聚类图标是自定义的,由我通过JavaScript(
JS()
函数)定义。 这样的地图应该是基于 radioButton()
中用户输入的反应式。因此,我使用了
observer()
和 if 语句,通过 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)
可能的解决方案是什么?
这个问题终于被郑乔解决了。解决方案在这里:
https://github.com/rstudio/leaflet/pull/696
确保通过以下方式安装更新的leaflet版本:
remotes::install_github("rstudio/leaflet")