Linked Questions

Popular Questions

How to add and group filters on a leaflet map?

Asked by At

I have a table of data that contains columns [ID, Status, Lat, Lon, Price 0BR, Price 1BR, Price 2BR, Price 3BR]. Currently I am mapping the ID by Lat/Lon in a leaflet map and want to filter by IDs that have a Price for 0BR, 1BR, 2BR, 3BR. It's worth noting that an ID may have null values for any one of the Price columns.

As it stands, I'm able to get the filter to appear, but I am not able to actually filter any data. I believe I need to have my code call out the IDs, but I'm not sure how.

df <- read.csv(file="MPLS_Saturation_Map.csv", header=TRUE, sep=",")
getColor <- function(df) {
sapply(df$Status, function(Status) {if(Status == '80% Undersaturated') {"red"} else if(Status == '50% Undersaturated') {"orange"} else {"green"}

icons <- awesomeIcons(
   icon = 'home',
   iconColor = 'black',
   library = 'ion',
   markerColor = getColor(df)

popup <- paste(
   "<strong>Rental ID: </strong>", df$Rental.ID, 
   "<br><strong>Total Units: </strong>", df$Units, 
   "<br><strong>Availability %: </strong>", df$Availability, 
   "<br><strong>Relative Price 0BR: </strong>", df$X0BR,
   "<br><strong>Relative Price 1BR: </strong>", df$X1BR,
   "<br><strong>Relative Price 2BR: </strong>", df$X2BR,
   "<br><strong>Relative Price 3BR: </strong>", df$X3BR,
   "<br><strong>Link: </strong>", df$Slug

pal <- colorFactor(c("orange", "red", "chartreuse3"),df$Status)

leaflet(df) %>% 
addTiles() %>%
    overlayGroups = c(if(length(df$X0BR)){"Studio"}, if(length(df$X1BR)){"1BR"}, if(length(df$X2BR)){"2BR"}, if(length(df$X3BR)){"3BR"}),
    options = layersControlOptions(collapsed = FALSE)) %>%
addLegend(pal = pal, title = '2018 Saturation Status', values = df$Status, opacity = 2, position = 'bottomright') %>%
addAwesomeMarkers(~Lon, ~Lat, icon = icons, popup = popup)

The initial results I had showed every price in the filter, but then I added >if(length(df$X0BR)){"Studio"}< etc. to get the proper filter labels ("Studio","1BR","2BR","3BR"). Now I just need to filter the dataset based on whether an ID has a price in df$X0BR, df$X1BR, df$X2BR, and df$X3BR.

Related Questions