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)
Try the below (I've unquoted some of the variables in df
as otherwise the app crashes):
library(tidyverse)
library(shiny)
library(leaflet)
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 <- (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) %>%
addCircles(lng = map_data() %>% pull(Y), lat = map_data() %>% pull(X), weight = 10,
radius = sqrt(map_data() %>% pull(Number_Total))*15000,
popup = map_data() %>% pull(Species))
})
}
shinyApp(ui = ui, server = server)
You need to pull the values directly from the reactive
context; I'd also suggest to do this in one statement rather than spreading it around.
PS In case your initial dataframe actually does contain all the variables in character
format, you can add just in case the mutate_at
statement in map_data
part, like this:
map_data <- reactive({
#req(input$species)
df[df$Species %in% input$species, ] %>%
mutate_at(vars(c("Number_Total", "X", "Y")), funs(as.numeric))
Then the script functions also with your initial dataframe provided here without manual changes.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.