在Shiny中,我制作了一个带有聚类标记的传单地图,聚类图标是一个自定义的,由我通过JavaScript (JS()
函数)定义。这样的映射应该是响应的基于用户在radioButton()
中的输入。因此,我使用了带有if语句的observer()
,通过leafletProxy()
更新映射。
当集群的图标是自定义的,而不是传单的默认,标记集群甚至不出现。似乎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
确保安装更新后的宣传单版本:
remotes::install_github("rstudio/leaflet")