Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R Shiny Leaflet map with checkboxGroupInput only displays two markers at a time

I’m trying to code an app that allows users to select multiple groups of points via checkboxGroupInput and have a leaflet map display the associated points with corresponding lat/longs. My dataframe is as follows:

x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")

Using this dataframe, I’d expect two points to appear for each species, and up to six points to appear at once if all three species boxes are checked. However, I’ve been unable to get more than two points at a time to display. If only one box at a time is checked, the points are correct, but when two or more boxes are checked, one point from each of two categories is displayed and the other points are missing. My code is as follows:

library(shiny)
library(leaflet)

#create dataframe 
x3 = structure(list(
     species = c("Duck", "Duck", "Goose", "Goose", "Swan", "Swan"), 
     lat = c(42, 43, 47, 38, 40, 40), 
     long = c(-110, -88, -122, -77, -112, -109)), 
     row.names = c(NA, 6L), class = "data.frame")
  
#setup user interface
ui = fluidPage(
  
  titlePanel("Bird Map"),
  
  sidebarLayout(
    
    sidebarPanel = sidebarPanel(
      
      checkboxGroupInput(inputId = "species1", 
                         label = "Species", 
                         choices = list("Duck",
                                        "Goose",
                                        "Swan"),
                         selected = "Duck"),
    ),
    
 #Main panel appearance    
    mainPanel = mainPanel(
      leafletOutput(outputId = 'map')
    )
  )
)

#Reactive server function
server = function(input, output){
  map_x3 = reactive({
    x3 %>%
      filter(species == input$species1) 
  })
  
  #Leaflet map output 
  output$map = renderLeaflet({
    leaflet(map_x3()) %>%
      addTiles() %>%
      setView(lng = -90, lat = 36.2, zoom = 4)%>%
      clearShapes() %>%
      addMarkers(label = ~ species)
  })
}

#Run app
shinyApp(ui = ui, server = server)

Thank you in advance, your help is appreciated.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I’ve experimented with different ways to display the data, simplified and more complex datasets, an observe () section, and additional reactive expressions. The problem still occurred, although with larger datasets I was often able to get more than two points to be displayed at once (although all points were never visible when multiple boxes were checked).

>Solution :

You should replace x3 %>% filter(species == input$species1) with

x3 %>% 
    dplyr::filter(species %in% input$species1) 

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading