I have what may be a complicated question. I'm trying to build a filterable map with shiny. I'm using the selectizeGroupUI
feature because it allows you to easily filter inputs in a way that links all the columns together.
There are six geographic based columns, and the hierarchy from largest to smallest goes as such:
district - subdistrict - territory - region - turf - areas
The way I setup the filters does part of the job, but I want it to group_by
in accordance to the selected inputs as well, but with a downward hierarchy
For example: If I'm filtering by region, I want it to group_by
the turf tied to that region. If I'm filtering by district, it will group_by
subdistrict, and so on.
Make sense? Here is my script (and here is the link to the shapefile):
library(sf)
library(shiny)
library(shinyWidgets)
library(dplyr)
library(leaflet)
library(mapview)
df <-
ui <- fluidPage(
fluidRow(
column(
width = 10, offset = 1,
tags$h3("Select Geography"),
panel(
selectizeGroupUI(
id = "filters",
params = list(
District = list(inputId = "district", title = "District:"),
Subdistrict = list(inputId = "subdistrict", title = "Subdistrict:"),
Territory = list(inputId = "territory", title = "Territory:"),
Region = list(inputId = "region", title = "Region:"),
Turf = list(inputId = "turf", title = "Turf:"),
Areas = list(inputId = "areas", title = "Designated Areas:")
))
#), status = "primary"
),
leafletOutput("test")
)
)
)
server <- function(input, output, session) {
res_mod <- callModule(
module = selectizeGroupServer,
id = "filters",
data = df,
vars = c("district", "subdistrict", "territory","region", "turf", "areas")
)
output$test <- renderLeaflet({
x <- res_mod() %>% st_sf()
res <- x %>% filter(is.null(input$region) | region %in% input$region,
is.null(input$turf) | turf %in% input$turf,
is.null(input$areas) | areas %in% input$areas,
is.null(input$district) | district %in% input$district,
is.null(input$subdistrict) | subdistrict %in% input$district,
is.null(input$territory) | territory %in% input$territory) %>% mutate(sales = tidyr::replace_na(sales, 0)) %>%
group_by() %>% summarise(totals = sum(sales))
mapview(res, zcol = "totals")@map
})
}
shinyApp(ui, server)