0

I'm trying to filter markers on a map by using the Check Box Group Input.

  titlePanel("Toronto Auto Thefts"),
  leafletOutput ("map", width = "100%", height = "100%"), 

  absolutePanel( 
                checkboxGroupInput("checkGroup", h3("Week Day"), 
                                   choices = list("Monday" = 1,
                                                  "Tuesday" = 2,
                                                  "Wednesday" = 3,
                                                  "Thursday" = 4,
                                                  "Friday" = 5,
                                                  "Saturday" = 6,
                                                  "Sunday" = 7),
                                   selected = 1))
)

The ui part seems ok because the check box is showing up appropriately.

server <- function(input, output, session){
  #filtered <- reactive({data[data$occurrencedayofweek != input$checkGroup]}) 


  output$map <- renderLeaflet({
    leaflet(data = data) %>%
      addTiles() %>%
      addMarkers()
  })

  observe ({
    proxy <- leafletProxy("map", data = data)
    proxy %>% clearMarkers ()
    if (data$occurrencedayofweek %in% input$checkGroup) {
      proxy %>% addMarker()}
    else {
      proxy %>% clearMarkers() %>% clearControls()}
  })
  }

I believe it is the server that is causing problems, however I'm not sure how to fix it.

softsoap
  • 13
  • 3
  • If your input can contain multiple choices from the user, the filter condition should be something like `subset(data, !(occurrencedayofweek %in% input$checkGroup))`. Otherwise, the server logic might fail in case several choices or no choices are selected – Gregor de Cillia Dec 25 '19 at 16:52
  • @GregordeCillia I've changed the filter function, but I'm not getting an error saying that server.R is returning a list. – softsoap Dec 25 '19 at 17:15

1 Answers1

1

This is my approach to the problem. I couldn't tweak your code properly so I rewrote part of it. It's easy to follow I think. In addition I added sample data (always a good practice to add a slice of your dataset).

library(shiny)
library(leaflet)

# c("Monday","Tuesday","Wednesday", "Thursday", "Friday","Saturday","Sunday")

mydata <- data.frame(occurrencedayofweek = c(1:7),
                         longitude = c(10.47, 10.48, 10.49, 10.50,10.51, 10.52, 10.53),
                         latitude = c(45.76, 45.77, 45.78, 45.79, 45.80, 45.81, 45.82))

shinyApp(

ui <- fluidPage(

  leafletOutput ("map"),

  checkboxGroupInput("checkGroup", "Week Day",
                     choices = c("Monday" = 1,
                                    "Tuesday" = 2,
                                    "Wednesday" = 3,
                                    "Thursday" = 4,
                                    "Friday" = 5,
                                    "Saturday" = 6,
                                    "Sunday" = 7),
                     selected = 1)
),

server <- function(input, output, session){

  output$map <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
      addMarkers(data = mydata,
                 ~longitude,
                 ~latitude,
                 group = "mygroup")
  })

  mydata_filtered <- reactive(mydata[mydata$occurrencedayofweek %in% input$checkGroup, ])

  observeEvent(input$checkGroup, {
    leafletProxy("map", data = mydata_filtered()) %>%
      clearGroup ("mygroup") %>%
      addMarkers(lng =  ~longitude,
                 lat = ~latitude,
                 group = "mygroup")
  })

 }

)    
G. Cocca
  • 2,456
  • 1
  • 12
  • 13