First of all, my thanks to Shree, who cleaned and made my previous code more performant here
Anyhow, I have some code that enables a user to select one or more species (from selectizeInput). The application shows you the species distribution on the map.
Now, I am puzzled, because I cannot deselect species? Once distributions are plotted, they remain on the map and I cannot remove them anymore... I have been looking thoroughly, but unable to see it.. I am pretty new to shiny.. so probably an easy mistake?
All code below, THanks!!! JOnas
DATAFRAME
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
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)
})
}
Run the application
shinyApp(ui = ui, server = server)