Search code examples
rshinyr-leaflet

Filter in Leaflet map by colname !=0


I want to subset my data set and show only the rows which its column value is different than 0.

This is a fake data set very similar to mine:

library(dplyr)
library(tidyr)
library(leaflet)
library(data.table)

ID<-c("1","10","15")
Bar<-c("2","5","0")
School<-c("3","0","2")
lat<- c(40.43008, 40.42424, 40.43375)
lon<-c(-3.803114,-3.689486,-3.733934)
Results<-data.frame(ID,Bar,School,lat,lon)

As it can be appreciated There are 3 ID (1,10,5).

This is a simulation of the leaflet map I did for a shiny app:

ui <- fluidPage(
  mainPanel(
    tabsetPanel(
      tabPanel("Map",leafletOutput("map"))
    ),
    checkboxGroupInput(
      inputId = "profile_input",
      label="Choose a Profile of People",
      choices = c("Bar","School")
    )))
server <- function(input, output, session) {
  output$map<-renderLeaflet({
    map<-  leaflet(Results)
    map<- addTiles(map=map)
    map<- addCircleMarkers(map=map, lng=~lon,lat=~lat,data=Results)

    map
  })
}

shinyApp(ui, server)

What I need is the checkboxgroupinput() to filter my data according to "Bar" and "School" and just plot the ID that have a different value than 0.

So for example if I select option "Bar" :

ID "15" has value "0" for "Bar", then I dont want ID 15 to be plotted. But ID "1" & "10" have different values than 0, so I want these 2 IDs to be on the map.

Any ID of how can I do that? I have been struggling with this for a long time!!


Solution

  • One approach would be replacing 0 values with NAs. This will allow you to benefit from the functions that are written to handle NAs in this particular problem (and in other similar cases). Below is a working solution:

    # import required packages 
    library(shiny) 
    library(leaflet) 
    library(dplyr)
    
    # construct the dummy data frame 
    Results <- data_frame(   
      ID = c("1","10","15"),   
      Bar = c("2","5","0"),   
      School = c("3","0","2"),   
      lat =  c(40.43008, 40.42424, 40.43375),   
      lon = c(-3.803114,-3.689486,-3.733934) 
    )
    
    # replace 0 values with NAs in order to use na.omit() 
    Results[Results == 0] <- NA
    
    ui <- fluidPage(
    
      mainPanel(
    
        tabsetPanel(
          tabPanel(
            "Map",
            leafletOutput("map")
          )
        ),
    
        checkboxGroupInput(
          inputId = "profile_input",
          label="Choose a Profile of People",
          choices = c("Bar","School")
        )
    
      )  
    
    )
    
    server <- function(input, output, session) {
    
      clean_data <- reactive({
        # store the needed column names based on user input
        var <- c("ID", input$profile_input, "lon", "lat")
        # filter the data frame and omit rows that include NAs
        na.omit(Results[var])   
      })
    
      # insert the reactive output of clean_data() where needed below   
      output$map<-renderLeaflet({
        map <-  leaflet(clean_data())
        map <- addTiles(map = map)
        map <- addCircleMarkers(
        map = map, 
          lng = ~lon,
          lat = ~lat,
          data = clean_data()
        )
        map   
      })    
    
    }
    
    shinyApp(ui = ui, server = server)