0

I am creating a leaflet map with two different types of layers:

  • A layer for total rental households, and a layer for total borrowing households. This layer should be mutually exclusive: i.e., you should only be able to select one of these layers at a time (either show total rental households OR total borrowing households). This will effectively function as a base layer: you can only choose one.
  • A layer Sydney, and a layer for the Rest of NSW. This layer does not need to be mutually exclusive: i.e., can show both Sydney AND Rest of NSW at the same time. These will effectively function like overlays: you can choose as many as you like.

Currently, I have only figured out how to show these layers as four separate layers: Sydney Renters, Rest of NSW renters, Sydney Borrowers, Rest of NSW Borrowers. (see image)

I would like to have these as two separate layers, with a choice for Renters OR Borrowers, and a separate layer for Sydney AND/OR Rest of NSW.

Here is the code used to generate the map:

leaflet() %>%  

addProviderTiles(providers$CartoDB.Positron, group = "OSM (default)") %>%
 
addPolygons(data = sf_syd, fillColor = ~pal2(Renting), color="white", weight=1, fillOpacity=0.4, group="Sydney Renters",
             highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>% 

addPolygons(data = sf_nsw, fillColor = ~pal2(Renting), color="white", weight=1, fillOpacity=0.4, group="Rest of NSW Renters",
             highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>% 

addPolygons(data = sf_syd, fillColor = ~pal2(Borrowing), color="white", weight=1, fillOpacity=0.4, group="Sydney Borrowers",
             highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>% 

addPolygons(data = sf_nsw, fillColor = ~pal2(Borrowing), color="white", weight=1, fillOpacity=0.4, group="Rest of NSW Borrowers",
             highlight = highlightOptions(weight=2, color="black", fillOpacity = 0.7, bringToFront = TRUE)) %>% 

addLayersControl(baseGroups = c("OSM (default)"),
                   overlayGroups = c("Sydney Renters",
                                     "Rest of NSW Renters",
                                     "Sydney Borrowers",
                                     "Rest of NSW Borrowers"),
                   options = layersControlOptions(collapsed = FALSE)) %>%
  addLegend(data= sf_syd, pal = pal2, values = ~perc_rental_stress, opacity = 0.7, position = "bottomright", title = "Households of </br> Household Type 'X'") 

enter image description here

TBA
  • 1,921
  • 4
  • 13
  • 26

1 Answers1

0

What you want is a user interface to filter the data before it goes into leaflet. This is not possible in Rleaflet without a UI layer like shiny.

You can theoretically create a matrix of combinations and create a layergroup for each possibility but this will not look good when mapping.

A proper shiny based solution looks like this,

library(shiny)
library(leaflet)
library(tmap)
data(World)

ui <- fluidPage(
  leafletOutput("mymap"),
  p(),
  br(),
  checkboxInput("asia", label = "Asia", TRUE),
  checkboxInput("africa", label = "Africa", TRUE),
  radioButtons("value",choiceValues = c("life_exp","footprint"), choiceNames =c("life_exp","footprint"), label = "test", selected = "life_exp")
)

server <- function(input, output, session) {
  filtered_world <- reactive({
    temp <- World %>% filter(1==2)
    if(input$asia) { temp <- rbind(temp, World %>% filter(continent == 'Asia')) }
    if(input$africa) { temp <- rbind(temp, World %>% filter(continent == 'Africa')) }
    temp$value <- temp[[input$value]]
    temp
  })

  output$mymap <- renderLeaflet({
    data <- filtered_world()
    pal <- colorNumeric(palette = "Blues",domain = data$value)
    leaflet() %>%
      addProviderTiles(providers$CartoDB.PositronNoLabels) %>%
      addPolygons(data = data, fillColor = ~pal(value),
                  fillOpacity = 1, color = "black", weight = 1)
  })
}

shinyApp(ui, server)