Show/hide grouped layers in leaflet

leaflet

#1

I am using reactive & observe function to show/hide layers but the color function seems to override the if statements in the reactive function. I want to group my layers(WatersourceOnPlot & PipedWateronPlot as WaterAccess then FlushToilets,OtherImproved,Unimproved & open Defecation as Sanitation) & be able to hide/show layers using a color function. The problem with the code is that I have to create a selectInput for each layer for the color mapping to work. When I put two items on one selectInput the first item overrides the other when it is color mapped.DOWNLOAD SHAPE FILE HERE.
<

#loading libraries
library(shiny)
library(leaflet)
library(rgdal)

#loading shape file
mp<-readOGR(
  dsn="merge",
  layer="m1")

#Remove NAs from AreaType col
mp<-mp[!is.na(mp$AreaTyp),]

#ui
ui<-fluidPage(
  leafletOutput("map"),
  selectInput(inputId = "pop",
              label = " POPULATION:",
              choices = list(
                "All"=1,
                "Population Per Km2"=4
                #"< 15,000"=2
                # "15,001 - 30,000"=3 ,
                # ">30,001"=1
              )
  ),

  selectInput(inputId = "area1", 
              label = " AreaType:",
              choices = c(
                "All",

                unique(as.character(mp$AreaTyp))
              ) 
  ),
  selectInput(
    inputId = "pw",
    label = "Water Type:",
    choices = c(
      "All"=1,
      "Piped Water On Plot"=2,
      "Water source On Plot"=3
    )
  ),
  selectInput(
    inputId = "ws",
    label = "Water Type2:",
    choices = c(
      "All"=1,
      "Water source On Plot"=2
    )
  ),
  selectInput(
    inputId = "ft",
    label = "Sanitation1:",
    choices = c(
      "All"=1,
      "Flush Toilets"=2
    )
  ),
  selectInput(
    inputId = "oi",
    label = "Sanitation2:",
    choices = c(
      "All"=1,
      "Other Improved"=2
    )
  ),
  selectInput(
    inputId = "ui",
    label = "Sanitation3:",
    choices = c(
      "All"=1,
      "UnImproved"=2
    )
  ),
  selectInput(
    inputId = "od",
    label = "Sanitation4:",
    choices = c(
      "All"=1,
      "Open Defecation"=2
    )
  )
)

#server
server<-function(input,output){
  #INTERACTIVE MAPPING
  #colormapping
  pal<-colorFactor(rainbow(7),mp$AreaTyp)

  #reactive function for flush toilets
  fts<-reactive({
    dm<-mp
    if(input$ft==1){
      dm[dm$FlshTlt<=25,]
    }
    else if(input$ws==2)
    {
      dm[dm$FlshTlt>25&dm$FlshTlt<=50,]
    }

    return(dm)
  })

  #reactive function for water source on plot
  wsp<-reactive({
    dm<-mp
    if(input$ws==1){
      dm[dm$WtrSrOP<=25,]
    }
    else if(input$ws==2)
    {
      dm[dm$WtrSrOP>25&dm$WtrSrOP<=50,]
    }
    else if(input$ws==3)
    {
      dm[dm$WtrSrOP>50&dm$WtrSrOP<=75,]
    }
    else if(input$ws==4)
    {
      dm[dm$WtrSrOP>75,]
    }
    return(dm)
  })

  #reactive function for piped water on plot
  pwp<-reactive({
    dm<-mp
    if(input$pw==2){
      dm[dm$PpdWtrP<=25,]
    }
    else if(input$pw==3)
    {dm[dm$WtrSrOP<=25,]}
    return(dm)
  })

  #reactive function for population per km2 
  ppd<-reactive({
    dt<-mp
    if(input$pop==1){
      dt[dt$PpDnsty<=15000,]
    } else if(input$pop==3){
      dt[dt$PpDnsty>15000&dt$PpDnsty<=30000,]
    } else if(input$pop==4){
      dt[dt$PpDnsty>30000,]
    }
    return(dt)
  })

  #reactive function for areatype
  fdata<-reactive({
    data<-mp
    if(input$area1!="All"){
      data<-subset(data,AreaTyp %in% input$area1)

    }


    return(data)
  })



  output$map<-renderLeaflet({

    leaflet(mp) %>%

      #Initializing the map
      setView(lng=36.092245, lat=-00.292115,zoom=15)%>%

      #Base map
      #Add default OpenStreetMap map tiles
      addTiles(group = "default")%>%

      #Overlay map
      addPolygons(
        data = mp,
        fillColor = "blue",
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>Water Source On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty
        )

      ) 

  })


  observe({
    pal<-colorFactor(rainbow(7),mp$AreaTyp)

    leafletProxy("map",data=fdata()) %>%

      clearMarkers() %>%
      clearControls() %>%
      clearShapes()%>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        fillColor = ~pal(AreaTyp),
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>Water Source On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty
        )

      )%>%
      addLegend(title = "AreaType", position = "topleft",
                pal = pal, values = ~AreaTyp, opacity = 1)
  })

  observe({
    pal1 <- colorBin("plasma", mp$PpDnsty, 15, pretty = TRUE)
    leafletProxy("map",data=ppd()) %>%

      # clearMarkers() %>%
      clearControls() %>%
      clearShapes()%>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        fillColor = ~pal1(PpDnsty),
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>WaterSource On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty

        )

      )%>%
      addLegend(title = "Population Per km2", position = "topleft",
                pal = pal1, values = ~PpDnsty, opacity = 1)

  })
  observe({

    pal1 <- colorBin("plasma", mp$PpdWtrP, 5, pretty = TRUE)
    leafletProxy("map",data=pwp()) %>%

      # clearMarkers() %>%
      clearControls() %>%
      clearShapes()%>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        fillColor = ~pal1(PpdWtrP),
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>WaterSource On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty

        )

      )%>%
      addLegend(title = "Piped Water On Plot(%)", position = "topleft",
                pal = pal1, values = ~PpdWtrP, opacity = 1)

  })
  observe({

    pal1 <- colorBin("plasma", mp$WtrSrOP, 5, pretty = TRUE)
    leafletProxy("map",data=wsp()) %>%

      # clearMarkers() %>%
      clearControls() %>%
      clearShapes()%>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        fillColor = ~pal1(WtrSrOP),
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>WaterSource On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty

        )

      )%>%
      addLegend(title = " Water source On Plot(%)", position = "topleft",
                pal = pal1, values = ~WtrSrOP, opacity = 1)

  })
  observe({

    pal1 <- colorBin("plasma", mp$FlshTlt, 5, pretty = TRUE)
    leafletProxy("leaf",data=fts()) %>%

      # clearMarkers() %>%
      clearControls() %>%
      clearShapes()%>%
      addPolygons(
        weight = 1, smoothFactor = 0.5,
        opacity = 1.0, fillOpacity = 1.0,
        fillColor = ~pal1(FlshTlt),
        highlightOptions = highlightOptions(
          weight = 2,
          color = "red",
          fillOpacity = 0.7,
          bringToFront = TRUE
        ),
        label =~LIA,
        popup = ~paste("<strong>Area Type:</strong>",AreaTyp,
                       "<br>",
                       "<strong>Piped Water On Plot:</strong>",PpdWtrP,"%",
                       "<br>",
                       "<strong>WaterSource On Plot:</strong>",WtrSrOP,"%",
                       "<br>",
                       "<strong>Flash Toilets:</strong>",FlshTlt,"%",
                       "<br>",
                       "<strong>Other Improved:</strong>",OthrImp,"%",
                       "<br>",
                       "<strong>Unimproved:</strong>",Unmprvd,"%",
                       "<br>",
                       "<strong>Open Defecation:</strong>",OpnDfct,"%",
                       "<br>",
                       "<strong>Population Per km2:</strong>",PpDnsty

        )

      )%>%
      addLegend(title = "Flush Toilets(%)", position = "topleft",
                pal = pal1, values = ~FlshTlt, opacity = 1)

  })
}


#runApp
shinyApp(ui,server)

/>


#2

Each one of your observers clears all shapes and then adds shapes. Is that what you actually want? And what do you mean by "hide/show layers using a color function"?


#3

Yes the observers clears the shapes & adds new ones but that's not what I want. I want conditional filtering where when I select say a planned area type & piped water on plot, only the shapes meeting the condition are displayed on the map. I know the observer functions should be interlinked to achieve this but I don't know how.