From 6e0ad2ab30898cb0820822e172597a7a07936d3a Mon Sep 17 00:00:00 2001 From: Millicool Date: Sun, 7 Dec 2025 17:24:56 +0100 Subject: [PATCH] Add Section click detection --- IT Shiny App.R | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/IT Shiny App.R b/IT Shiny App.R index 38ed84d..61dca18 100644 --- a/IT Shiny App.R +++ b/IT Shiny App.R @@ -9,9 +9,12 @@ crime_json <- fromJSON(file="data.json") geo_bezirke <- st_read("geobezirke-parsed.json") geo_bezirke <- st_transform(geo_bezirke, crs = 4326) +geo_bezirke$leaflet_id <- paste("bez_", geo_bezirke$bezirk, sep="") + geo_stadtteile <- st_read("geostadtteile-parsed.json") geo_stadtteile <- st_transform(geo_stadtteile, crs = 4326) +geo_stadtteile$leaflet_id <- paste("std_", geo_stadtteile$stadtteil, sep="") bezirke <- names(crime_json) @@ -35,7 +38,9 @@ ui <- page_fillable( ) ), leafletOutput("hhmap"), - card("Card rechts"), + card( + textOutput("txt_map_selection") + ), col_widths = c(3, 6, 3) ) ) @@ -75,11 +80,26 @@ server <- function(input, output, session){ } }) + observeEvent(input$hhmap_shape_click, { + click_event <- input$hhmap_shape_click + # Check if an ID was returned (meaning a polygon was clicked) + if (!is.null(click_event$id)) { + + # The ID of the clicked polygon + clicked_polygon_id <- click_event$id + output$txt_map_selection <- renderText({ + clicked_polygon_id + }) + print(paste("Polygon with ID", clicked_polygon_id, "was clicked.")) + } + }) + output$hhmap <- renderLeaflet({ leaflet() %>% addProviderTiles(providers$CartoDB.Positron) %>% addPolygons( data = geo_bezirke, + layerId = ~leaflet_id, group = "layer_bezirke", color = "#7bb5ab", fillColor = "#bdf0e7", @@ -94,6 +114,7 @@ server <- function(input, output, session){ addPolygons( data = geo_stadtteile, group = "layer_stadtteile", + layerId = ~leaflet_id, color = "#7bb5ab", fillColor = "#bdf0e7", options = pathOptions(pane = "overlayPane"), # Use a leaflet option to ensure it's hidden