Merge pull request #1 from syounkin/sgy

Using R Markdown and make
This commit is contained in:
bvarick 2024-10-29 13:23:31 -05:00 committed by GitHub
commit f5897c8b4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 72 additions and 9 deletions

15
.gitignore vendored
View File

@ -1,8 +1,19 @@
.Rproj* .Rproj*
data/addresses/* data/addresses/*
api_keys/* api_keys/*
figures/* figures/*
.Rhistory .Rhistory
.Rproj.user .Rproj.user
*.md
.DS_Store
!README.md
.Rhistory
*\.\#*
data
!data/
.RData
data-bkup/
data-bkup
*.R
*.bak
archive/

15
Makefile Normal file
View File

@ -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

View File

@ -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(tidyverse)
library(ggmap) library(ggmap)
library(sf) library(sf)
@ -5,8 +19,13 @@ library(osrm)
library(smoothr) library(smoothr)
library(magick) library(magick)
library(ggnewscale) 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
school_focus <- data.frame(name = c("East High School"), NCES_CODE = c("550852000925")) 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") WI_schools <- st_read(dsn = "data/Schools/WI_schools.gpkg")
## load addresses ## 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) %>% filter(lat > 0) %>%
st_as_sf(coords=c("lon","lat"), crs=4326) # remember x=lon and y=lat 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 <- st_bbox(st_transform(st_buffer(addresses_near, dist = 500), crs = 4326))
bbox <- c(left = as.double(bbox[1]), bbox <- c(left = as.double(bbox[1]),
bottom = as.double(bbox[2]), bottom = as.double(bbox[2]),
right = as.double(bbox[3]), right = as.double(bbox[3]),
top = as.double(bbox[4])) top = as.double(bbox[4]))
#get basemap #get basemap
@ -118,7 +137,7 @@ ggmap(basemap) +
size = 2) size = 2)
ggsave(file = paste0("figures/", ggsave(file = paste0("figures/",
school_focus %>% pull(name), school_focus %>% pull(name),
" Routes.pdf"), " Routes.pdf"),
title = paste0(school_focus %>% pull(name), " Walking Routes"), title = paste0(school_focus %>% pull(name), " Walking Routes"),
device = pdf, device = pdf,
@ -165,7 +184,7 @@ ggmap(basemap) +
label.size = 0.04, label.size = 0.04,
size = 2) size = 2)
ggsave(file = paste0("figures/", ggsave(file = paste0("figures/",
school_focus %>% pull(name), school_focus %>% pull(name),
" Routes - Traffic Stress.pdf"), " Routes - Traffic Stress.pdf"),
title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"), title = paste0(school_focus %>% pull(name), " Walking Routes - Traffic Stress"),
device = pdf, device = pdf,
@ -209,7 +228,7 @@ ggmap(basemap) +
label.size = 0.04, label.size = 0.04,
size = 2) size = 2)
ggsave(file = paste0("figures/", ggsave(file = paste0("figures/",
school_focus %>% pull(name), school_focus %>% pull(name),
" Addresses.pdf"), " Addresses.pdf"),
title = paste0(school_focus %>% pull(name), " Addresses"), title = paste0(school_focus %>% pull(name), " Addresses"),
device = pdf, device = pdf,
@ -217,3 +236,13 @@ ggsave(file = paste0("figures/",
width = 11, width = 11,
units = "in", units = "in",
create.dir = TRUE) create.dir = TRUE)
```
# Appendix
```{r chunklast, eval = TRUE, echo = TRUE, results = "show", warning = TRUE, error = TRUE, message = TRUE}
date()
sessionInfo()
```

View File

@ -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 ### A map of those routes colored by the level of traffic stress to bike
![example routes-lts figure](examples/example-routes-lts.png) ![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*

4
html/.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
# Ignore everything in this directory
*
# Except this file
!.gitignore