From 817126bef5c91ef3bc69f8f5d4d1a3483b61531d Mon Sep 17 00:00:00 2001 From: syounkin Date: Tue, 29 Oct 2024 10:39:05 -0500 Subject: [PATCH 1/5] Run Markdown Script with make Created a Makefile to be used to run R markdown scripts. See README for more info. Also created R and html subdirectories. The repo should contain the html subdirectory but not the files in it. --- .gitignore | 15 ++++++- Makefile | 15 +++++++ route_analysis.R => R/route_analysis.Rmd | 43 ++++++++++++++++--- .../route_analysis.Rproj | 0 html/.gitignore | 4 ++ 5 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 Makefile rename route_analysis.R => R/route_analysis.Rmd (91%) rename route_analysis.Rproj => R/route_analysis.Rproj (100%) create mode 100644 html/.gitignore diff --git a/.gitignore b/.gitignore index 599f426..b349772 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,19 @@ .Rproj* - data/addresses/* api_keys/* figures/* - .Rhistory .Rproj.user +*.md +.DS_Store +!README.md +.Rhistory +*\.\#* +data +!data/ +.RData +data-bkup/ +data-bkup +*.R +*.bak +archive/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..70d7bb1 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +route_analysis: R/route_analysis.Rmd + R -e 'library("rmarkdown"); old_path <- Sys.getenv("PATH"); Sys.setenv(PATH = paste(old_path, "/usr/local/bin", sep = ":")); rmarkdown::render(knit_root_dir = "~/route_analysis/", output_dir = "~/route_analysis/html", input = "./R/route_analysis.Rmd", output_file = "./html/route_analysis.html")' + +clean: clean-data clean-figure clean-script + +clean-data: + rm -vf ./R/data/*.rds + +clean-script: + rm -rvf ./*.md + +clean-figure: + rm -rvf ./figure/ + +.PHONY: data diff --git a/route_analysis.R b/R/route_analysis.Rmd similarity index 91% rename from route_analysis.R rename to R/route_analysis.Rmd index 24ad135..dfe65bc 100644 --- a/route_analysis.R +++ b/R/route_analysis.Rmd @@ -1,3 +1,17 @@ +--- +title: "Route Analysis" +output: + html_document: + toc: true + toc_depth: 5 + toc_float: + collapsed: false + smooth_scroll: true +--- + +```{r libs, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} +date() +rm(list=ls()) library(tidyverse) library(ggmap) library(sf) @@ -5,8 +19,13 @@ library(osrm) library(smoothr) library(magick) library(ggnewscale) +fig.height <- 6 +set.seed(1) +``` +# Main R script +```{r Rscript, eval = FALSE, echo = TRUE, results = "show", warning = FALSE, error = FALSE, message = FALSE} ## school focus school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925")) @@ -17,7 +36,7 @@ walk_boundary_m <- 1.5 * 1609 WI_schools <- st_read(dsn = "data/Schools/WI_schools.gpkg") ## load addresses -addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>% +addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>% filter(lat > 0) %>% st_as_sf(coords=c("lon","lat"), crs=4326) # remember x=lon and y=lat @@ -71,9 +90,9 @@ school_symbol <- image_read_svg(path = "other/school_FILL0_wght400_GRAD0_opsz24. bbox <- st_bbox(st_transform(st_buffer(addresses_near, dist = 500), crs = 4326)) -bbox <- c(left = as.double(bbox[1]), - bottom = as.double(bbox[2]), - right = as.double(bbox[3]), +bbox <- c(left = as.double(bbox[1]), + bottom = as.double(bbox[2]), + right = as.double(bbox[3]), top = as.double(bbox[4])) #get basemap @@ -118,7 +137,7 @@ ggmap(basemap) + size = 2) ggsave(file = paste0("figures/", - school_focus %>% pull(name), + school_focus %>% pull(name), " Routes.pdf"), title = paste0(school_focus %>% pull(name), " Walking Routes"), device = pdf, @@ -165,7 +184,7 @@ ggmap(basemap) + label.size = 0.04, size = 2) ggsave(file = paste0("figures/", - school_focus %>% pull(name), + school_focus %>% pull(name), " Routes - Traffic Stress.pdf"), title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"), device = pdf, @@ -209,7 +228,7 @@ ggmap(basemap) + label.size = 0.04, size = 2) ggsave(file = paste0("figures/", - school_focus %>% pull(name), + school_focus %>% pull(name), " Addresses.pdf"), title = paste0(school_focus %>% pull(name), " Addresses"), device = pdf, @@ -217,3 +236,13 @@ ggsave(file = paste0("figures/", width = 11, units = "in", create.dir = TRUE) + + +``` + +# Appendix + +```{r chunklast, eval = TRUE, echo = TRUE, results = "show", warning = TRUE, error = TRUE, message = TRUE} +date() +sessionInfo() +``` diff --git a/route_analysis.Rproj b/R/route_analysis.Rproj similarity index 100% rename from route_analysis.Rproj rename to R/route_analysis.Rproj diff --git a/html/.gitignore b/html/.gitignore new file mode 100644 index 0000000..5e7d273 --- /dev/null +++ b/html/.gitignore @@ -0,0 +1,4 @@ +# Ignore everything in this directory +* +# Except this file +!.gitignore From 3b74670fd1aa3c7d1eb2f83b9216997ced784d40 Mon Sep 17 00:00:00 2001 From: syounkin Date: Tue, 29 Oct 2024 10:43:27 -0500 Subject: [PATCH 2/5] Update to README.md --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 2e4d3e9..5354ae9 100644 --- a/README.md +++ b/README.md @@ -15,3 +15,7 @@ This script will generate a few figures: ### A map of those routes colored by the level of traffic stress to bike ![example routes-lts figure](examples/example-routes-lts.png) + +## Using make +The command `make route_analysis` will run *route_analysis.Rmd* which +is an R markdown file containing the original R script *route_analysis.R* From 027d2b6f5d8973aab5db3452f0e415a0ac36c10f Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Tue, 29 Oct 2024 13:35:51 -0500 Subject: [PATCH 3/5] adjusted where the html file is saved --- Makefile | 2 +- R/route_analysis.Rmd | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 70d7bb1..bfc6f18 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ route_analysis: R/route_analysis.Rmd - R -e 'library("rmarkdown"); old_path <- Sys.getenv("PATH"); Sys.setenv(PATH = paste(old_path, "/usr/local/bin", sep = ":")); rmarkdown::render(knit_root_dir = "~/route_analysis/", output_dir = "~/route_analysis/html", input = "./R/route_analysis.Rmd", output_file = "./html/route_analysis.html")' + R -e 'library("rmarkdown"); old_path <- Sys.getenv("PATH"); Sys.setenv(PATH = paste(old_path, "/usr/local/bin", sep = ":")); rmarkdown::render(knit_root_dir = "./", output_dir = "./html", input = "./R/route_analysis.Rmd", output_file = "./html/route_analysis.html")' clean: clean-data clean-figure clean-script diff --git a/R/route_analysis.Rmd b/R/route_analysis.Rmd index dfe65bc..8a4d3b1 100644 --- a/R/route_analysis.Rmd +++ b/R/route_analysis.Rmd @@ -106,7 +106,7 @@ ggmap(basemap) + x = NULL, y = NULL, color = NULL, - linewidth = "How many students can use road") + + linewidth = "How many students would use road") + theme(axis.text=element_blank(), axis.ticks=element_blank(), plot.caption = element_text(color = "grey")) + @@ -154,7 +154,7 @@ ggmap(basemap) + x = NULL, y = NULL, color = NULL, - linewidth = "How many students can use road") + + linewidth = "How many students would use road") + theme(axis.text=element_blank(), axis.ticks=element_blank(), plot.caption = element_text(color = "grey")) + From f8cf08ef8b3a9d442fda96beb49a1c124abd53a0 Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Tue, 29 Oct 2024 16:18:08 -0500 Subject: [PATCH 4/5] adjusted directories to allow me to work line by line in RStudio --- R/route_analysis.Rmd | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/R/route_analysis.Rmd b/R/route_analysis.Rmd index 8a4d3b1..b920db1 100644 --- a/R/route_analysis.Rmd +++ b/R/route_analysis.Rmd @@ -25,7 +25,7 @@ set.seed(1) # Main R script -```{r Rscript, eval = FALSE, echo = TRUE, results = "show", warning = FALSE, error = FALSE, message = FALSE} +```{r Rscript, eval = TRUE, echo = TRUE, results = "show", warning = FALSE, error = TRUE, message = FALSE} ## school focus school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925")) @@ -33,10 +33,10 @@ school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("55085200 walk_boundary_m <- 1.5 * 1609 ## load school locations -WI_schools <- st_read(dsn = "data/Schools/WI_schools.gpkg") +WI_schools <- st_read(dsn = "../data/Schools/WI_schools.gpkg") ## load addresses -addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>% +addresses <- read_csv(file="../data/addresses/Addresses_Students_EastHS_2024_GeocodeResults.csv") %>% filter(lat > 0) %>% st_as_sf(coords=c("lon","lat"), crs=4326) # remember x=lon and y=lat @@ -44,7 +44,7 @@ addresses <- read_csv(file="data/addresses/Addresses_Students_EastHS_2024_Geocod options(osrm.server = "http://127.0.0.1:5000/") options(osrm.profile = "walk") -register_stadiamaps(key = substr(read_file(file = "api_keys/stadia_api_key"), 1, 36)) +register_stadiamaps(key = substr(read_file(file = "../api_keys/stadia_api_key"), 1, 36)) ## subset addresses within 1.5 miles walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance( @@ -56,7 +56,7 @@ walk_boundary_poly <- fill_holes(st_make_valid(osrmIsodistance( addresses_near <- st_intersection(addresses, walk_boundary_poly) ## load bike tls -bike_lts <- st_read("data/bike_lts/bike_lts_DANE.geojson") +bike_lts <- st_read("../data/bike_lts/bike_lts_DANE.geojson") bike_lts[["lts"]] <- as.factor(bike_lts$LTS_F) bike_lts_scale <- data.frame(code = c(1, 2, 3, 4, 9), @@ -85,8 +85,8 @@ bike_lts <- st_join(bike_lts, bike_lts_buffer %>% select(OBJECTID, student_use)) ## make maps # load logo -logo <- image_read(path = "other/BFW_Logo_180_x_200_transparent_background.png") -school_symbol <- image_read_svg(path = "other/school_FILL0_wght400_GRAD0_opsz24.svg") +logo <- image_read(path = "../other/BFW_Logo_180_x_200_transparent_background.png") +school_symbol <- image_read_svg(path = "../other/school_FILL0_wght400_GRAD0_opsz24.svg") bbox <- st_bbox(st_transform(st_buffer(addresses_near, dist = 500), crs = 4326)) @@ -136,7 +136,7 @@ ggmap(basemap) + label.size = 0.04, size = 2) -ggsave(file = paste0("figures/", +ggsave(file = paste0("../figures/", school_focus %>% pull(name), " Routes.pdf"), title = paste0(school_focus %>% pull(name), " Walking Routes"), @@ -150,7 +150,7 @@ ggsave(file = paste0("figures/", ggmap(basemap) + labs(title = paste0("Walking routes for students at ", school_focus %>% pull(name)), - subtitle = "only showing routes within the 1.5 walk boundary", + subtitle = "only showing routes within the walk boundary", x = NULL, y = NULL, color = NULL, @@ -183,7 +183,7 @@ ggmap(basemap) + nudge_y = 0.0015, label.size = 0.04, size = 2) -ggsave(file = paste0("figures/", +ggsave(file = paste0("../figures/", school_focus %>% pull(name), " Routes - Traffic Stress.pdf"), title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"), @@ -227,7 +227,7 @@ ggmap(basemap) + nudge_y = 0.0015, label.size = 0.04, size = 2) -ggsave(file = paste0("figures/", +ggsave(file = paste0("../figures/", school_focus %>% pull(name), " Addresses.pdf"), title = paste0(school_focus %>% pull(name), " Addresses"), From 96cdcbc5ecdc820c4429c3d44f9ca46a33c37519 Mon Sep 17 00:00:00 2001 From: Ben Varick Date: Tue, 29 Oct 2024 16:21:57 -0500 Subject: [PATCH 5/5] adjusted wording, adjusted buffer for line intersections from 10m to 20m --- R/route_analysis.Rmd | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/route_analysis.Rmd b/R/route_analysis.Rmd index b920db1..f391e28 100644 --- a/R/route_analysis.Rmd +++ b/R/route_analysis.Rmd @@ -77,7 +77,7 @@ for(i in addresses_near$number) { routes <- bind_rows(routes) ## combine routes -bike_lts_buffer <- st_buffer(st_intersection(bike_lts, walk_boundary_poly), 10) +bike_lts_buffer <- st_buffer(st_intersection(bike_lts, walk_boundary_poly), 20) bike_lts_buffer["student_use"] <- unlist(lapply(st_intersects(bike_lts_buffer, routes), length)) @@ -106,7 +106,7 @@ ggmap(basemap) + x = NULL, y = NULL, color = NULL, - linewidth = "How many students would use road") + + linewidth = "Potential student walkers") + theme(axis.text=element_blank(), axis.ticks=element_blank(), plot.caption = element_text(color = "grey")) + @@ -154,7 +154,7 @@ ggmap(basemap) + x = NULL, y = NULL, color = NULL, - linewidth = "How many students would use road") + + linewidth = "Potential student walkers") + theme(axis.text=element_blank(), axis.ticks=element_blank(), plot.caption = element_text(color = "grey")) +