首先,感谢Shree清理和优化了我的之前的代码(在这里)
无论如何,我有一些代码,使用户能够从selectizeInput中选择一个或多个物种。应用程序会在地图上显示该物种的分布情况。
现在,我困惑了,因为我不能取消选择物种?一旦分布被绘制出来,它们就会保留在地图上,我不能再将其移除... 我已经仔细查找了,但是无法看到它.. 我对Shiny非常陌生.. 所以可能是一个简单的错误?
以下是全部代码, 谢谢!!! JOnas
数据框
df<- data.frame(
Number_Total = sample(c("5", "6", "1", "3")),
Species = sample(c("Ilione trifaria", "Pherbellia argyrotarsis",
"Euthycera seguyi", "Ilione trifaria")),
X= sample(c("37", "28", "21", "30")),
Y= sample(c("-5", "-16", "-10", "-15"))
)
UI
ui <- (fluidPage(titlePanel("Species Checker"),
sidebarLayout(
sidebarPanel(
selectizeInput('species', 'Choose species',
choices = df$Species, multiple = TRUE,
options = list(placeholder = 'select species'))
),
mainPanel(
leafletOutput("CountryMap", width = 600, height = 600))
)
))
服务器
server <- function(input, output, session) {
map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ]
})
output$CountryMap <- renderLeaflet({
leaflet() %>% addTiles() %>%
setView(lng = 20, lat = 40, zoom = 2)
})
map_proxy <- leafletProxy("CountryMap")
observe({
md <- map_data()
map_proxy %>%
addCircles(lng = md$Y, lat = md$X, weight = 10,
radius = sqrt(md$Number_Total)*15000, popup = md$Species)
})
}
运行应用程序
shinyApp(ui = ui, server = server)