【发布时间】:2020-10-10 23:21:28
【问题描述】:
我想用 Shiny 创建一个交互式传单地图。创建工作正常,但尽管 selectInput() 发生了变化并单击“开始!”旧标记不会被删除,也不会设置新标记。你能帮帮我吗?
操作系统: Windows 10 64 位 工作室 R 版本:R 版本 4.0.0
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)
【问题讨论】:
标签: r shiny shinydashboard r-leaflet