diff --git a/app.R b/app.R index 24d0f4b..c8924c7 100644 --- a/app.R +++ b/app.R @@ -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() %>% @@ -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 %>% @@ -235,13 +261,13 @@ 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) } @@ -249,9 +275,11 @@ server <- function(input, output, session){ 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")), @@ -262,13 +290,19 @@ 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") %>% @@ -276,6 +310,9 @@ server <- function(input, output, session){ 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({