knitr::opts_chunk$set(echo = FALSE, comment = NA, warning = FALSE, error = FALSE, message = FALSE, tidy = TRUE, fig.dim = c(10, 6), global.par = TRUE)
library(tidyverse) # includes tidyr, tibble, ggplots, dbplyr # library(data.table) # db files are loaded as a data table library(wisdotcrashdatabase) # library(janitor) # make tabyl, adorn_totals # source("C:/temp_code/chart_formats.R") source("C:/temp_code/functions_to_find_intersection_crashes.R")
db_dir = "C:/data/crashes_duck.duckdb" # location of crash database files to load years = get_list_of_years("18", "22") # select years to import, must be in this format
crash <- import_db_data( db_dir, db_type = "crash", years = years, columns = c("RLTNTRWY", "MUNICODE", "MUNITYPE", "ONHWY", "ONSTR", "CRSHLOC", "PEDFLAG", "INTTYPE", "INTDIS", "CRSHMTH", "TOTINJ", "TOTFATL", "LONDECDG", "LATDECDG"), filter_by = "county:Milwaukee" )
# city = crash[MUNICODE %in% find_municipality_codes("Milwaukee", "City")$MuniCode & CRSHSVR != "Property Damage"] peds = crash |> filter(PEDFLAG == "Y") |> get_crash_location() |> filter(crash_location == "intersection") peds_sf = crash_to_sf(peds) cl = cluster_crashes(peds_sf) top_10 = top_clusters(cl, 5)
ped_top_10 = subset(cl,clust %in% top_10$clust) ped_top_10 |> mapview::mapview(zcol = "clust", layer.name = "cluster ID")
ped_top_10_df = sf::st_drop_geometry(ped_top_10 ) |> as.data.frame() |> factor_crash_variables() aggregate_crashes_by_crshsvr_tot_inj(ped_top_10_df, "People injured/killed in these crashes by cluster ID")
# library(sf) # other method is better since this method buffer could be 2+ intersections # circles are either too small (2 circles at an intersection) or too large (crash circles cover 2+ intersections) (20 and 40 m, respectively) # p2 = st_transform(to_map, crs = 3701) # st_crs(p2)$units # p2_buff = st_buffer(p2, 20) # # p2_union = st_union(p2_buff) # p2_cast = st_cast(p2_union, "POLYGON") # # p2_st = sf::st_as_sf(p2_cast) |> mutate(ID = seq(1, 3386)) # # inter = st_intersection(p2, p2_st) # # inter = st_intersects(p2_st, p2) # mapview::mapview(p2_union) # # inter |> as.data.frame() |> count(ID) |> top_n(wt = n, n = 10)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.