fix: improve filtering logic and map updates in Shiny app

Co-authored-by: aider (deepseek/deepseek-chat) <aider@aider.chat>
This commit is contained in:
2026-03-13 06:29:44 -04:00
parent e0cf03df2f
commit 1b75760496

106
app.R
View File

@@ -359,50 +359,72 @@ server <- function(input, output, session) {
} }
}) })
filteredSbdvn <- reactive({ # Initialize filteredOwners with all owners
if (is.null(input$sub_name) || input$sub_name == "All") {
return(unique(sbdvn$max_sub_id))
} else {
return(
sbdvn %>%
filter(sub_name == input$sub_name) %>%
pull(max_sub_id)
)
}
})
filteredOwners <- reactiveVal(owners) filteredOwners <- reactiveVal(owners)
# Update filtered owners when filter button is clicked
observeEvent(input$filterButton, { observeEvent(input$filterButton, {
filtered_owners <- # Start with all owners
owners %>% filtered <- owners
filter(subdivision %in% filteredSbdvn()) %>%
# Filter by subdivision if specified
if (!is.null(input$sub_name) && input$sub_name != "All") {
# Get the subdivision IDs for the selected subdivision name
sub_ids <- sbdvn %>%
filter(sub_name == input$sub_name) %>%
pull(max_sub_id)
filtered <- filtered %>%
filter(subdivision %in% sub_ids)
}
# Filter by name if specified
if (!is.null(input$name) && input$name != "") {
filtered <- filtered %>%
filter( filter(
grepl(input$name, owner_1, ignore.case = TRUE) | grepl(input$name, owner_1, ignore.case = TRUE) |
grepl(input$name, owner_2, ignore.case = TRUE) grepl(input$name, owner_2, ignore.case = TRUE)
) %>% )
filter(grepl(input$location, location, ignore.case = TRUE)) }
filteredOwners(filtered_owners) # Filter by location if specified
if (!is.null(input$location) && input$location != "") {
filtered <- filtered %>%
filter(grepl(input$location, location, ignore.case = TRUE))
}
filteredOwners(filtered)
})
# Also update when the app starts
observe({
# Trigger initial update
filteredOwners(owners)
}) })
mean_lat <- reactive({ mean_lat <- reactive({
if (!is.null(filteredOwners()) && nrow(filteredOwners()) > 0) {
filteredOwners() %>% filteredOwners() %>%
st_coordinates() %>% st_coordinates() %>%
.[, "Y"] %>% .[, "Y"] %>%
mean() mean()
} else {
27.076199
}
}) })
mean_lng <- reactive({ mean_lng <- reactive({
if (!is.null(filteredOwners()) && nrow(filteredOwners()) > 0) {
filteredOwners() %>% filteredOwners() %>%
st_coordinates() %>% st_coordinates() %>%
.[, "X"] %>% .[, "X"] %>%
mean() mean()
} else {
-82.362253
}
}) })
output$map <- renderLeaflet({ output$map <- renderLeaflet({
if (!is.null(filteredOwners()) && nrow(filteredOwners()) > 0) {
leaflet() %>% leaflet() %>%
addProviderTiles("CartoDB.Voyager") %>% addProviderTiles("CartoDB.Voyager") %>%
addPolygons( addPolygons(
@@ -413,10 +435,15 @@ server <- function(input, output, session) {
fillOpacity = 0.2, fillOpacity = 0.2,
label = ~sub_name, label = ~sub_name,
group = "Subdivisions" group = "Subdivisions"
) %>% )
})
# Update map markers when filteredOwners changes
observe({
leafletProxy("map") %>%
clearGroup("Owners") %>%
addMarkers( addMarkers(
data = filteredOwners(), data = filteredOwners(),
#color = ~ifelse(homestead == 1, "green", "red"),
popup = popupTable( popup = popupTable(
filteredOwners(), filteredOwners(),
row.numbers = FALSE, row.numbers = FALSE,
@@ -434,25 +461,10 @@ server <- function(input, output, session) {
options = layersControlOptions(collapsed = FALSE) options = layersControlOptions(collapsed = FALSE)
) %>% ) %>%
setView(lng = mean_lng(), lat = mean_lat(), zoom = 16) setView(lng = mean_lng(), lat = mean_lat(), zoom = 16)
} else {
leaflet() %>%
addProviderTiles("CartoDB.Voyager") %>%
addPolygons(
data = sbdvn,
color = "red",
weight = 2,
opacity = 0.5,
fillOpacity = 0.2,
label = ~sub_name,
group = "Subdivisions"
) %>%
setView(lng = -82.362253, lat = 27.076199, zoom = 16)
}
}) })
output$table <- renderDT({ output$table <- renderDT({
if (!is.null(filteredOwners()) && nrow(filteredOwners()) > 0) {
my_table <- my_table <-
filteredOwners() %>% filteredOwners() %>%
st_drop_geometry() %>% st_drop_geometry() %>%
@@ -468,6 +480,26 @@ server <- function(input, output, session) {
dom = 'tpi' dom = 'tpi'
) )
) )
} else {
# Return empty table with same structure
my_table <- data.frame(
label = character(0),
owner_1 = character(0),
owner_2 = character(0),
homestead = numeric(0)
)
datatable(my_table,
colnames = c("Address", "Owner 1", "Owner 2", "Homestead"),
rownames = FALSE,
options = list(
pageLength = 10,
scrollX = TRUE,
searching = FALSE,
lengthMenu = c(5, 10, 25, 50),
dom = 'tpi'
)
)
}
}) })
prep_mailing <- function(data) { prep_mailing <- function(data) {
data |> data |>