0


I am very new to shiny, and I have a question.
I have a simple dataset with observations (Number_Total) of species (Species), in a certain location (X,Y).

I would like to generate a map, that enables you to select the species in a dropdown menu. Shiny then shows you were the species occurs on the map.

I got pretty far (for my experience), but selecting species in the menu does not do anything...

ui <- (fluidPage(titlePanel("Species Checker"),  
                 sidebarLayout(
                   sidebarPanel(
                      selectizeInput('species', 'Choose species', 
                   choices    = df$Species, multiple = TRUE)
                     ),
                   mainPanel(
                     leafletOutput("CountryMap", 
               width = 1000, height = 500))
                 )
))

The server side

server <- function(input, output, session){
  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      setView(lng = 10, lat = 40, zoom = 5) %>%
      addCircles(lng = df$Y, lat = df$X, weight = 10, 
      radius =sqrt(df$Number_Total)*15000, popup = df$Species)
  })


  observeEvent(input$species, {

    if(input$species != "")
    {
      leafletProxy("CountryMap") %>% clearShapes()
      index = which(df$Species == input$species)
      leafletProxy("CountryMap")%>% addCircles(lng = df$X[index], 
      lat = df$Y[index],
                                               weight = 1, 
     radius =sqrt(df$Number_Total[index])*30, popup = df$Species[index])
    }
  }) 

}

And finally plot it

shinyApp(ui = ui, server = server)

I know my code is probably messy, but again, I blaim my experience =) I did not manage to get an example dataset in here right away, so here it comes as picture

This is the result of the above code (with slightly different data) enter image description here

Shree
  • 10,835
  • 1
  • 14
  • 36
jonas
  • 39
  • 5

1 Answers1

1

Here's what you need. I think you are skilled enough to understand this but comment if you have any questions.

server <- function(input, output, session) {
  # map_data <- reactive({
    # req(input$species)
    # df[df$Species %in% input$species, ]
  # })

  output$CountryMap <- renderLeaflet({
    leaflet() %>% addTiles() %>% 
      setView(lng = 10, lat = 40, zoom = 5)
  })

  map_proxy <- leafletProxy("CountryMap")

  observe({
    md <- df[df$Species %in% input$species, ]
    map_proxy %>%
      addCircles(lng = md$Y, lat = md$X, weight = 10, 
      radius = sqrt(md$Number_Total)*15000, popup = md$Species)
  })
}
Shree
  • 10,835
  • 1
  • 14
  • 36
  • Aha yes, that's a great help indeed! I understand the code. Now I can select species, that will be shown on my map. If I deselect a species, that species remains plotted on my map. I have been trying to figure it out, but not succesfull so far :-) I'll give an update soon :-) Yet again, thanks! – jonas Nov 08 '18 at 08:49
  • Consider marking it correct if it solved your problem. – Shree Nov 08 '18 at 14:14
  • Hm, well, I am still struggling to 'reset' the map. I'll comment below :-) Soon solved I hope :-) – jonas Nov 09 '18 at 07:24
  • okay, I have edited the answer and commented out `req(input$species)` in `map_data` reactive. This should solve your problem. Let me know. – Shree Nov 09 '18 at 14:53
  • Unfortunately, it does not change? The species that are plotted, remain plotted... I am sorry, and yet again, Thanks! – jonas Nov 10 '18 at 13:23
  • Try the new edit. Share the output of `dput(head(df))` i.e. your input data so that I can test and fix the solution myself. – Shree Nov 10 '18 at 13:25
  • Ah yes, indeed, the new edits have done the job! Great! Thank you very much for your efforts, appreciated! – jonas Nov 11 '18 at 09:18