Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions app.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,41 @@ server <- function(input, output, session){
})

#create empty vector to hold all click ids
selected <- reactiveValues(groups = vector())
selected <- reactiveValues(groups = vector(),
counter = 0)

#reset selected tracts when tract_year changes
observeEvent(tract_year_reactive(), {

#print(names(selected))

selected$groups <- NULL

})

observeEvent(input$map_shape_click, {
print("observing click on basemap")
print(input$map_shape_click$group)
print(input$map_shape_click$id)
print(selected$groups)
#print(selected_tracts_geo_reactive()$GEOID)
#print(str_remove(input$map_shape_click$id, "^Tract "))

print(str_c("Was ", selected$counter, sep = ""))
if (input$map_shape_click$group == "base_map"){

selected$counter <- selected$counter + 1

} else {

selected$counter <- selected$counter -1

}

print(str_c("Now is ", selected$counter, sep = ""))

})

#initial map output
output$map <- renderLeaflet({
leaflet() %>%
Expand Down Expand Up @@ -219,7 +245,7 @@ server <- function(input, output, session){
if(input$map_shape_click$group == "base_map"){
#when the user clicks a polygon on the basemap, add that polygon to selected$groups and display the new layer
selected$groups <- c(selected$groups, str_remove(input$map_shape_click$id, "^Tract ")) #remove "Tract " from start of id on the fly

leaflet_pal <- colorFactor(palette_reactive(), selected_tracts_geo_reactive()$GEOID)

proxy %>%
Expand All @@ -235,23 +261,25 @@ server <- function(input, output, session){
label = ~GEOID)
} else if(input$map_shape_click$group == "hover_polygon") {
#when the user clicks on a tract that is highlighted by plotly already, clear that highlight polygon from the map

proxy %>% clearGroup("hover_polygon")

} else {
#when the user clicks a tract that is already in selected$groups, remove that tract from selected$groups and remove it from the second layer
selected$groups <- setdiff(selected$groups, input$map_shape_click$group)

proxy %>% clearGroup(input$map_shape_click$group)

}
}, ignoreInit = TRUE)

observeEvent(plotly_hover_event_reactive(), {

print("highlighting tract on map based on plotly hover")

leaflet_pal <- colorFactor(palette_reactive(), selected_tracts_geo_reactive()$GEOID)

proxy %>%
output <- proxy %>%
clearGroup("hover_polygon") %>%
addPolygons(data = ac_tracts_reactive() %>%
semi_join(plotly_hover_event_reactive(), by = c("GEOID" = "customdata")),
Expand All @@ -262,20 +290,29 @@ server <- function(input, output, session){
label = ~GEOID,
group = "hover_polygon")

print("finished highlight")

return(output)

})

geoid_table_reactive <- reactive({

req(length(selected$groups) > 0)

selected$groups %>%
#print("Join selected$groups to data_source_reactive()")

output <- selected$groups %>%
enframe(value = "GEOID") %>%
select(-name) %>%
left_join(st_drop_geometry(ac_tracts_reactive()), by = "GEOID") %>%
select(NAME, GEOID) %>%
left_join(data_source_reactive()) %>%
filter(between(year, input$year_slider[1], input$year_slider[2]))

#print("finished join")

return(output)
})

output$geoid_table <- DT::renderDataTable({
Expand Down