带有闪亮-clearMarkers()的交互式传单,并设置新传单
我想创建一个互动传单地图与闪亮。创建工作正常,但尽管在selectInput()和单击“go!”时进行了更改,但不会删除旧标记,也不会设置新标记。你能帮帮我吗 操作系统: Windows 10 64位 斯图迪奥 R版本:R版本4.0.0带有闪亮-clearMarkers()的交互式传单,并设置新传单,r,shiny,shinydashboard,r-leaflet,R,Shiny,Shinydashboard,R Leaflet,我想创建一个互动传单地图与闪亮。创建工作正常,但尽管在selectInput()和单击“go!”时进行了更改,但不会删除旧标记,也不会设置新标记。你能帮帮我吗 操作系统: Windows 10 64位 斯图迪奥 R版本:R版本4.0.0 geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"), &
geocodes <- data.frame("customer" = c("John", "Ahmet", "Zara"),
"longitude" = c(8.71, 8.59, 8.98),
"latitude" = c(51.2, 51.3, 51.1))
# UI
ui <- dashboardPage(
dashboardHeader(title = "CustomerLocation Dashboard"),
dashboardSidebar(
selectInput("account", label = "Account",
choices = c(unique(geocodes$customer)),
multiple = TRUE),
actionButton("go", "Go!")
),
dashboardBody(
tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"),
leafletOutput("mymap", height=1000)
)
)
# server
server <- function(input, output, session){
reactiveDf <- reactive({
if(is.null(input$account)){
geocodes
} else{
geocodes[geocodes$customer %in% input$account,]
}
})
output$mymap <- renderLeaflet({
leaflet(geocodes) %>%
addProviderTiles("OpenStreetMap",
group = "OpenStreetMap"
) %>%
addAwesomeMarkers(
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
eventReactive(input$go,{
leafletProxy("mymap", data = reactiveDf()) %>%
clearShapes() %>%
clearPopups() %>%
clearMarkers() %>%
addAwesomeMarkers(
data = reactiveDf(),
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
}
shinyApp(ui, server)
geocodes%
clearMarkers()%>%
添加体标记(
data=reactiveDf(),
lng=经度,
纬度=~纬度,
图标=自定义图标,
clusterOptions=markerClusterOptions(),
标签=~客户,
弹出=~popup
)
})
}
shinyApp(用户界面、服务器)
我已经确定了三个修复程序:
c(unique(geocodes$customer))
将此输入转换为c(1,2,3)
。当您稍后尝试用c(1,2,3)将客户名称“John”、“Ahmet”和“Zara”子集时,这是不兼容的,并且失败了selectInput("account", label = "Account",
choices = unique(geocodes$customer),
multiple = TRUE),
output$mymap <- renderLeaflet({
leaflet(reactiveDf()) %>%
addProviderTiles("OpenStreetMap",
group = "OpenStreetMap"
) %>%
addAwesomeMarkers(
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
proxy()
中的clearMarkers()
不会像您期望的那样清除标记,而是将原始地图从使用geocodes
渲染更改为reactiveDf()
(如果input$account
为空,它仍然返回geocodes
数据帧)似乎解决了这个问题output$mymap <- renderLeaflet({
leaflet(geocodes) %>%
addProviderTiles("OpenStreetMap",
group = "OpenStreetMap"
) %>%
addAwesomeMarkers(
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
output$mymap <- renderLeaflet({
leaflet(reactiveDf()) %>%
addProviderTiles("OpenStreetMap",
group = "OpenStreetMap"
) %>%
addAwesomeMarkers(
lng = ~longitude,
lat = ~latitude,
icon = customIcon,
clusterOptions = markerClusterOptions(),
label = ~customer,
popup = ~popup
)
})
observeEvent(input$go, {
if(is.null(input$account)){
geocodes = geocodes
} else{
geocodes = geocodes[geocodes$customer %in% input$account,]
}
leafletProxy("mymap") %>%
clearShapes() %>%
clearPopups() %>%
clearMarkers() %>%
addMarkers(
data = geocodes,
lng = ~longitude,
lat = ~latitude,
label = ~customer,
)
}
)