From dd90137f52f4c03b7eaa7398a93b7a9ffc8a5d2b Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Thu, 12 Feb 2026 11:10:22 +0000 Subject: [PATCH 1/3] Add ai-generated qmd --- walking-safety-herefordshire.qmd | 88 ++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 walking-safety-herefordshire.qmd diff --git a/walking-safety-herefordshire.qmd b/walking-safety-herefordshire.qmd new file mode 100644 index 00000000..46e3d769 --- /dev/null +++ b/walking-safety-herefordshire.qmd @@ -0,0 +1,88 @@ +--- +title: "Walking safety in Herefordshire" +format: html +execute: + message: false + warning: false +--- + +## Overview + +This document imports STATS19 collision data, filters to collisions involving pedestrians in Herefordshire, and shows an interactive map with `tmap` in view mode. + +## Setup + +```{python} +x = 1 +``` + +```{r} +library(dplyr) +library(sf) +library(stats19) +library(tmap) +``` + +## Data import + +```{r} +# Download collision and casualty data (latest available year). +collisions_2023 = get_stats19(year = 2023, type = "accidents", ask = FALSE) +casualties_2023 = get_stats19(year = 2023, type = "cas", ask = FALSE) + +# Keep collisions that involve at least one pedestrian casualty. +pedestrian_collisions = casualties_2023 |> + filter(casualty_type == "Pedestrian") |> + distinct(accident_index) + +collisions_2023 = collisions_2023 |> + semi_join(pedestrian_collisions, by = "accident_index") + +# Convert collisions to sf points. +collisions_sf = format_sf(collisions_2023) +``` + +## Herefordshire boundary + +```{r} +# Load LAD boundaries and keep Herefordshire. +lad_boundaries = read_sf( + "https://github.com/ITSLeeds/tds/releases/download/2025/lad_boundaries_2023.geojson" +) + +herefordshire = lad_boundaries |> + filter(grepl("Herefordshire", LAD23NM)) |> + st_union() |> + st_as_sf() + +# Clip collision points to Herefordshire. +collisions_herefordshire = st_filter(collisions_sf, herefordshire) +``` + +## Interactive map + +```{r} +# Create an interactive map. +tmap_mode("view") + +tm_shape(herefordshire) + + tm_borders(lwd = 2, col = "#2c3e50") + + tm_shape(collisions_herefordshire) + + tm_dots( + # Commented out because it was generating this issue: +#[tm_dots()] Argument `title` unknown. +# Error in `tm_dots()`: +# ! Visual values used for the variable "fill" +# are incorrect. +# ℹ Variable should a data variable name or a single +# color. +# Run `rlang::last_trace()` to see where the error occurred. + # col = "accident_severity", + # palette = "Reds", + # size = 0.05, + # alpha = 0.7, + # title = "Pedestrian collisions (2023)" + ) + # + + # tm_basemap("OpenStreetMap") +``` From 002d27410a2f41c8cb22b197cd908e859616bff1 Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Thu, 12 Feb 2026 11:22:27 +0000 Subject: [PATCH 2/3] Fix issues in code --- walking-safety-herefordshire.qmd | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/walking-safety-herefordshire.qmd b/walking-safety-herefordshire.qmd index 46e3d769..be9d7a90 100644 --- a/walking-safety-herefordshire.qmd +++ b/walking-safety-herefordshire.qmd @@ -4,6 +4,7 @@ format: html execute: message: false warning: false +# note: use ctrl+Shift+k to render --- ## Overview @@ -31,14 +32,16 @@ collisions_2023 = get_stats19(year = 2023, type = "accidents", ask = FALSE) casualties_2023 = get_stats19(year = 2023, type = "cas", ask = FALSE) # Keep collisions that involve at least one pedestrian casualty. -pedestrian_collisions = casualties_2023 |> - filter(casualty_type == "Pedestrian") |> - distinct(accident_index) +# Commented out because next line is failing +# TODO: fix this issue and re-enable the code. +# pedestrian_collisions = casualties_2023 |> +# filter(casualty_type == "Pedestrian") |> +# distinct(accident_index) -collisions_2023 = collisions_2023 |> - semi_join(pedestrian_collisions, by = "accident_index") +# collisions_2023 = collisions_2023 |> +# semi_join(pedestrian_collisions, by = "accident_index") -# Convert collisions to sf points. +# # Convert collisions to sf points. collisions_sf = format_sf(collisions_2023) ``` @@ -65,8 +68,17 @@ collisions_herefordshire = st_filter(collisions_sf, herefordshire) # Create an interactive map. tmap_mode("view") -tm_shape(herefordshire) + - tm_borders(lwd = 2, col = "#2c3e50") + +# 5. └─tmap:::step4_plot(...) +# 6. ├─base::do.call(...) +# 7. ├─tmap::tmapLeafletDataPlot(...) +# 8. ├─tmap:::tmapLeafletDataPlot.tm_data_borders(...) +# 9. ├─base::NextMethod() +# 10. └─tmap:::tmapLeafletDataPlot.tm_data_polygons(...) + +# Quitting from walking-safety-herefordshire.qmd:67-91 [unnamed-chunk-5] +# Execution halted +# tm_shape(herefordshire) + +# tm_borders(lwd = 2, col = "#2c3e50") + tm_shape(collisions_herefordshire) + tm_dots( # Commented out because it was generating this issue: From db95f5cadc82ab5a46b411228b4f390fd131a4ef Mon Sep 17 00:00:00 2001 From: Robin Lovelace Date: Thu, 12 Feb 2026 11:22:53 +0000 Subject: [PATCH 3/3] Original generation --- walking-safety-herefordshire.qmd | 59 +++++++++----------------------- 1 file changed, 17 insertions(+), 42 deletions(-) diff --git a/walking-safety-herefordshire.qmd b/walking-safety-herefordshire.qmd index be9d7a90..1bdf6119 100644 --- a/walking-safety-herefordshire.qmd +++ b/walking-safety-herefordshire.qmd @@ -4,7 +4,6 @@ format: html execute: message: false warning: false -# note: use ctrl+Shift+k to render --- ## Overview @@ -13,10 +12,6 @@ This document imports STATS19 collision data, filters to collisions involving pe ## Setup -```{python} -x = 1 -``` - ```{r} library(dplyr) library(sf) @@ -29,19 +24,17 @@ library(tmap) ```{r} # Download collision and casualty data (latest available year). collisions_2023 = get_stats19(year = 2023, type = "accidents", ask = FALSE) -casualties_2023 = get_stats19(year = 2023, type = "cas", ask = FALSE) +casualties_2023 = get_stats19(year = 2023, type = "casualties", ask = FALSE) # Keep collisions that involve at least one pedestrian casualty. -# Commented out because next line is failing -# TODO: fix this issue and re-enable the code. -# pedestrian_collisions = casualties_2023 |> -# filter(casualty_type == "Pedestrian") |> -# distinct(accident_index) +pedestrian_collisions = casualties_2023 |> + filter(casualty_type == "Pedestrian") |> + distinct(accident_index) -# collisions_2023 = collisions_2023 |> -# semi_join(pedestrian_collisions, by = "accident_index") +collisions_2023 = collisions_2023 |> + semi_join(pedestrian_collisions, by = "accident_index") -# # Convert collisions to sf points. +# Convert collisions to sf points. collisions_sf = format_sf(collisions_2023) ``` @@ -54,7 +47,7 @@ lad_boundaries = read_sf( ) herefordshire = lad_boundaries |> - filter(grepl("Herefordshire", LAD23NM)) |> + filter(grepl("Herefordshire", lad23nm)) |> st_union() |> st_as_sf() @@ -68,33 +61,15 @@ collisions_herefordshire = st_filter(collisions_sf, herefordshire) # Create an interactive map. tmap_mode("view") -# 5. └─tmap:::step4_plot(...) -# 6. ├─base::do.call(...) -# 7. ├─tmap::tmapLeafletDataPlot(...) -# 8. ├─tmap:::tmapLeafletDataPlot.tm_data_borders(...) -# 9. ├─base::NextMethod() -# 10. └─tmap:::tmapLeafletDataPlot.tm_data_polygons(...) - -# Quitting from walking-safety-herefordshire.qmd:67-91 [unnamed-chunk-5] -# Execution halted -# tm_shape(herefordshire) + -# tm_borders(lwd = 2, col = "#2c3e50") + +tm_shape(herefordshire) + + tm_borders(lwd = 2, col = "#2c3e50") + tm_shape(collisions_herefordshire) + tm_dots( - # Commented out because it was generating this issue: -#[tm_dots()] Argument `title` unknown. -# Error in `tm_dots()`: -# ! Visual values used for the variable "fill" -# are incorrect. -# ℹ Variable should a data variable name or a single -# color. -# Run `rlang::last_trace()` to see where the error occurred. - # col = "accident_severity", - # palette = "Reds", - # size = 0.05, - # alpha = 0.7, - # title = "Pedestrian collisions (2023)" - ) - # + - # tm_basemap("OpenStreetMap") + col = "accident_severity", + palette = "Reds", + size = 0.05, + alpha = 0.7, + title = "Pedestrian collisions (2023)" + ) + + tm_basemap("OpenStreetMap") ```