Add Radio Button functionality to change map type

This commit is contained in:
2025-12-07 16:22:18 +01:00
parent 58c9e42a1e
commit eae7e7bff3

View File

@@ -6,13 +6,24 @@ library(sf)
crime_json <- fromJSON(file="C:\\Users\\milli\\Documents\\Datenbereinigung\\data.json") crime_json <- fromJSON(file="C:\\Users\\milli\\Documents\\Datenbereinigung\\data.json")
geojson_data <- st_read("C:\\Users\\milli\\Documents\\Datenbereinigung\\geojson.json")
geojson_data <- st_transform(geojson_data, crs = 4326) geo_bezirke <- st_read("C:\\Users\\milli\\Documents\\Datenbereinigung\\geobezirke-parsed.json")
geo_bezirke <- st_transform(geo_bezirke, crs = 4326)
geo_stadtteile <- st_read("C:\\Users\\milli\\Documents\\Datenbereinigung\\geostadtteile-parsed.json")
geo_stadtteile <- st_transform(geo_stadtteile, crs = 4326)
bezirke <- names(crime_json) bezirke <- names(crime_json)
ui <- page_fillable( ui <- page_fillable(
layout_columns( layout_columns(
card( card(
radioButtons(
inputId = "rd_maptype",
label = "Kartentyp",
choices = c("Bezirke", "Stadtteile"),
selected = "Bezirke"
),
selectInput( selectInput(
inputId = "drp_bezirk", inputId = "drp_bezirk",
label = "Bezirk", label = "Bezirk",
@@ -49,21 +60,52 @@ server <- function(input, output, session){
} }
}) })
observeEvent(input$rd_maptype, {
maptype <- input$rd_maptype
mapproxy <- leafletProxy("hhmap")
if (maptype == "Bezirke"){
hideGroup(mapproxy, "layer_stadtteile")
showGroup(mapproxy, "layer_bezirke")
}
else {
hideGroup(mapproxy, "layer_bezirke")
showGroup(mapproxy, "layer_stadtteile")
}
})
output$hhmap <- renderLeaflet({ output$hhmap <- renderLeaflet({
leaflet() %>% leaflet() %>%
addProviderTiles(providers$CartoDB.Positron) %>% addProviderTiles(providers$CartoDB.Positron) %>%
addPolygons( addPolygons(
data = geojson_data, data = geo_bezirke,
group = "layer_bezirke",
color = "#7bb5ab", color = "#7bb5ab",
fillColor = "#bdf0e7", fillColor = "#bdf0e7",
weight = 1, weight = 1,
fillOpacity = 0.05, # Polygon fill transparency fillOpacity = 0.4, # Polygon fill transparency
highlightOptions = highlightOptions( highlightOptions = highlightOptions(
color = "#103b57", color = "#103b57",
weight = 4, weight = 4,
bringToFront = TRUE bringToFront = TRUE
), ),
) %>% ) %>%
addPolygons(
data = geo_stadtteile,
group = "layer_stadtteile",
color = "#7bb5ab",
fillColor = "#bdf0e7",
options = pathOptions(pane = "overlayPane"), # Use a leaflet option to ensure it's hidden
weight = 1,
fillOpacity = 0.4, # Polygon fill transparency
highlightOptions = highlightOptions(
color = "#103b57",
weight = 4,
bringToFront = TRUE
),
) %>%
setView( setView(
lng = 9.98716634776887, lng = 9.98716634776887,
lat = 53.5488439196432, lat = 53.5488439196432,
@@ -71,5 +113,7 @@ server <- function(input, output, session){
) )
}) })
} }
options(shiny.host = '0.0.0.0')
options(shiny.port = 8888)
shinyApp(ui = ui, server = server) shinyApp(ui = ui, server = server)