├── .gitignore ├── LICENSE ├── data └── delay_codes.rda ├── reports └── 2019 │ ├── .DS_Store │ └── February │ ├── delays_February_2019_clean.rds │ ├── delays_February_2019_raw.xlsx │ ├── 01-setup.R │ ├── 03-delays_report.Rmd │ └── 02-data_cleaning.R ├── .Rbuildignore ├── figure ├── unnamed-chunk-1-1.png └── unnamed-chunk-2-1.png ├── NAMESPACE ├── data-raw ├── Subway & SRT Log Codes.xlsx └── delay_codes.R ├── man ├── data_cleaning.Rd ├── setup.Rd └── delays_report.Rd ├── delaysreport.Rproj ├── DESCRIPTION ├── R ├── data_cleaning.R ├── setup.R └── delays_report.R ├── README.md ├── README.Rmd ├── inst └── templates │ ├── setup.R │ ├── delays_report.Rmd │ └── data_cleaning.R ├── LICENSE.md └── .Rhistory /.gitignore: -------------------------------------------------------------------------------- 1 | .Rproj.user 2 | *.DS_Store 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2019 2 | COPYRIGHT HOLDER: Sharla Gelfand 3 | -------------------------------------------------------------------------------- /data/delay_codes.rda: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/data/delay_codes.rda -------------------------------------------------------------------------------- /reports/2019/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/reports/2019/.DS_Store -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^delaysreport\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^LICENSE\.md$ 4 | ^data-raw$ 5 | ^README\.Rmd$ 6 | -------------------------------------------------------------------------------- /figure/unnamed-chunk-1-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/figure/unnamed-chunk-1-1.png -------------------------------------------------------------------------------- /figure/unnamed-chunk-2-1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/figure/unnamed-chunk-2-1.png -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | export(data_cleaning) 4 | export(delays_report) 5 | export(setup) 6 | -------------------------------------------------------------------------------- /data-raw/Subway & SRT Log Codes.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/data-raw/Subway & SRT Log Codes.xlsx -------------------------------------------------------------------------------- /reports/2019/February/delays_February_2019_clean.rds: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/reports/2019/February/delays_February_2019_clean.rds -------------------------------------------------------------------------------- /reports/2019/February/delays_February_2019_raw.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sharlagelfand/delaysreport/HEAD/reports/2019/February/delays_February_2019_raw.xlsx -------------------------------------------------------------------------------- /man/data_cleaning.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/data_cleaning.R 3 | \name{data_cleaning} 4 | \alias{data_cleaning} 5 | \title{Template for data cleaning} 6 | \usage{ 7 | data_cleaning(report_month, report_year) 8 | } 9 | \arguments{ 10 | \item{report_month}{Month of report} 11 | 12 | \item{report_year}{Year of report} 13 | } 14 | \description{ 15 | Opens a template of steps for cleaning the month's delays data. 16 | } 17 | -------------------------------------------------------------------------------- /man/setup.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/setup.R 3 | \name{setup} 4 | \alias{setup} 5 | \title{Set up delays report} 6 | \usage{ 7 | setup(report_month, report_year) 8 | } 9 | \arguments{ 10 | \item{report_month}{Month of report} 11 | 12 | \item{report_year}{Year of report} 13 | } 14 | \description{ 15 | Set up delays report, with instructions on how to get the data and helper functions to clean the data and write the report. 16 | } 17 | -------------------------------------------------------------------------------- /delaysreport.Rproj: -------------------------------------------------------------------------------- 1 | Version: 1.0 2 | 3 | RestoreWorkspace: No 4 | SaveWorkspace: No 5 | AlwaysSaveHistory: Default 6 | 7 | EnableCodeIndexing: Yes 8 | UseSpacesForTab: Yes 9 | NumSpacesForTab: 2 10 | Encoding: UTF-8 11 | 12 | RnwWeave: Sweave 13 | LaTeX: pdfLaTeX 14 | 15 | AutoAppendNewline: Yes 16 | StripTrailingWhitespace: Yes 17 | 18 | BuildType: Package 19 | PackageUseDevtools: Yes 20 | PackageInstallArgs: --no-multiarch --with-keep.source 21 | PackageRoxygenize: rd,collate,namespace 22 | -------------------------------------------------------------------------------- /man/delays_report.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/delays_report.R 3 | \name{delays_report} 4 | \alias{delays_report} 5 | \title{Template for creating delays report} 6 | \usage{ 7 | delays_report(report_month, report_year, render = FALSE) 8 | } 9 | \arguments{ 10 | \item{report_month}{Month of report} 11 | 12 | \item{report_year}{Year of report} 13 | 14 | \item{render}{Should the report be rendered automatically? Defaults to `FALSE`} 15 | } 16 | \description{ 17 | Opens a template of the delays report for the month, optionally rendering it. 18 | } 19 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: delaysreport 2 | Title: TTC delays report 3 | Version: 0.0.0.9000 4 | Authors@R: 5 | person(given = "Sharla", 6 | family = "Gelfand", 7 | role = c("aut", "cre"), 8 | email = "sharla.gelfand@gmail.com") 9 | Description: Monthly report on top 5 causes for delay for each 10 | TTC line. 11 | License: MIT + file LICENSE 12 | Encoding: UTF-8 13 | LazyData: true 14 | RoxygenNote: 6.1.1 15 | Imports: 16 | usethis, 17 | fs, 18 | readxl, 19 | dplyr, 20 | here, 21 | janitor, 22 | assertr, 23 | ggplot2, 24 | rmarkdown 25 | -------------------------------------------------------------------------------- /R/data_cleaning.R: -------------------------------------------------------------------------------- 1 | #' Generate template for data cleaning 2 | #' 3 | #' @description Opens a template of steps for cleaning the month's delays data. 4 | #' 5 | #' @param report_month Month of report 6 | #' @param report_year Year of report 7 | #' 8 | #' @export 9 | data_cleaning <- function(report_month, report_year){ 10 | report_path <- fs::path("reports", report_year, report_month) 11 | 12 | usethis::use_template(template = "data_cleaning.R", 13 | save_as = paste0(report_path, "/02-data_cleaning.R"), 14 | data = list(report_month = report_month, 15 | report_year = report_year), 16 | package = "delaysreport", 17 | open = TRUE) 18 | } 19 | -------------------------------------------------------------------------------- /R/setup.R: -------------------------------------------------------------------------------- 1 | #' Set up delays report 2 | #' 3 | #' @description Set up delays report, with instructions on how to get the data and helper functions to clean the data and write the report. 4 | #' 5 | #' @param report_month Month of report 6 | #' @param report_year Year of report 7 | #' 8 | #' @export 9 | setup <- function(report_month, report_year){ 10 | report_path <- fs::path("reports", report_year, report_month) 11 | usethis::use_directory(report_path) 12 | 13 | usethis::use_template(template = "setup.R", 14 | save_as = paste0(report_path, "/01-setup.R"), 15 | data = list(report_month = report_month, 16 | report_year = report_year), 17 | package = "delaysreport", 18 | open = TRUE) 19 | } 20 | -------------------------------------------------------------------------------- /reports/2019/February/01-setup.R: -------------------------------------------------------------------------------- 1 | # Top 5 delays by TTC Line for February 2019 2 | 3 | # Step 1: Download the data. 4 | # Go to https://portal0.cf.opendata.inter.sandbox-toronto.ca/dataset/ttc-subway-delay-data/ 5 | # Download this month's data. Save it in /reports/2019/February/ 6 | # Name it delays_February_2019_raw.xlsx 7 | 8 | # Step 2: Clean the data 9 | delaysreport::data_cleaning(report_month = "February", 10 | report_year = "2019") 11 | 12 | # Step 3: Run the report! 13 | # Set render to TRUE if you would like the report to render automatically; keep it as FALSE if you want the .Rmd file to open and to render it yourself. 14 | delaysreport::delays_report(report_month = "February", 15 | report_year = "2019", 16 | render = TRUE) 17 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | # delaysreport 5 | 6 | The goal of delaysreport is to help automate monthly reporting on the 7 | top 5 causes for delay on each TTC subway line. 8 | 9 | It is a toy package intended to accompany [this 10 | blogpost](https://sharla.party/posts/usethis-for-reporting/) on using 11 | `usethis`. 12 | 13 | ## Installation 14 | 15 | You can install the development version of delaysreport from GitHub 16 | with: 17 | 18 | ``` r 19 | # install.packages("devtools") 20 | devtools::install_github("sharlagelfand/delaysreport") 21 | ``` 22 | 23 | ## Reporting 24 | 25 | To begin the delays analysis for a given month, use 26 | `delaysreport::setup()`, e.g.: 27 | 28 | ``` r 29 | delaysreport::setup(report_month = "January", 30 | report_year = "2019") 31 | ``` 32 | -------------------------------------------------------------------------------- /reports/2019/February/03-delays_report.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Top 5 delays, February 2019" 3 | output: html_document 4 | --- 5 | 6 | ```{r setup, echo=FALSE} 7 | library(dplyr) 8 | library(ggplot2) 9 | library(here) 10 | 11 | delays <- readRDS(here("reports", "2019", "February", "delays_February_2019_clean.rds")) 12 | ``` 13 | 14 | ```{r, echo=FALSE} 15 | delays %>% 16 | group_by(line, description) %>% 17 | summarise(delays = sum(min_delay)) %>% 18 | arrange(-delays) %>% 19 | slice(1:5) %>% 20 | ggplot(aes(x = description, 21 | y = delays)) + 22 | geom_col() + 23 | scale_x_discrete("") + 24 | scale_y_continuous("Delay (minutes)") + 25 | ggtitle("Top 10 causes for delay, by line", 26 | subtitle = "February 2019") + 27 | coord_flip() + 28 | facet_wrap(vars(line), 29 | nrow = 1) + 30 | theme_minimal() 31 | ``` 32 | -------------------------------------------------------------------------------- /data-raw/delay_codes.R: -------------------------------------------------------------------------------- 1 | library(dplyr) 2 | library(readxl) 3 | library(janitor) 4 | 5 | delay_codes <- read_excel("data-raw/Subway & SRT Log Codes.xlsx") %>% 6 | clean_names() 7 | 8 | delay_codes <- delay_codes %>% 9 | select(code = sub_rmenu_code, description = code_description_3) %>% 10 | remove_empty("rows") %>% 11 | bind_rows( 12 | delay_codes %>% 13 | select(code = srt_rmenu_code, description = code_description_7) %>% 14 | remove_empty("rows") 15 | ) 16 | 17 | # Add codes that are in data, but not code lookup 18 | 19 | missing_codes <- tribble( 20 | ~code, ~description, 21 | "MUNCA", "No Collector Available - Non E.S.A. Related", 22 | "TRNCA", "No Collector Available", 23 | "PUEO", "Delay Description Unknown" 24 | ) 25 | 26 | delay_codes <- delay_codes %>% 27 | bind_rows(missing_codes) 28 | 29 | usethis::use_data(delay_codes) 30 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | # delaysreport 8 | 9 | The goal of delaysreport is to help automate monthly reporting on the top 5 causes for delay on each TTC subway line. 10 | 11 | It is a toy package intended to accompany [this blogpost](https://sharla.party/posts/usethis-for-reporting/) on using `usethis`. 12 | 13 | ## Installation 14 | 15 | You can install the development version of delaysreport from GitHub with: 16 | 17 | ``` r 18 | # install.packages("devtools") 19 | devtools::install_github("sharlagelfand/delaysreport") 20 | ``` 21 | 22 | ## Reporting 23 | 24 | To begin the delays analysis for a given month, use `delaysreport::setup()`, e.g.: 25 | 26 | ```{r, eval=FALSE} 27 | delaysreport::setup(report_month = "January", 28 | report_year = "2019") 29 | ``` 30 | -------------------------------------------------------------------------------- /inst/templates/setup.R: -------------------------------------------------------------------------------- 1 | # Top 5 delays by TTC Line for {{{ report_month }}} {{{ report_year }}} 2 | 3 | # Step 1: Download the data. 4 | # Go to https://portal0.cf.opendata.inter.sandbox-toronto.ca/dataset/ttc-subway-delay-data/ 5 | # Download this month's data. Save it in /reports/{{{ report_year }}}/{{{ report_month }}}/ 6 | # Name it delays_{{{ report_month }}}_{{{ report_year }}}_raw.xlsx 7 | 8 | # Step 2: Clean the data 9 | delaysreport::data_cleaning(report_month = "{{{ report_month }}}", 10 | report_year = "{{{ report_year }}}") 11 | 12 | # Step 3: Run the report! 13 | # Set render to TRUE if you would like the report to render automatically; keep it as FALSE if you want the .Rmd file to open and to render it yourself. 14 | delaysreport::delays_report(report_month = "{{{ report_month }}}", 15 | report_year = "{{{ report_year }}}", 16 | render = FALSE) 17 | -------------------------------------------------------------------------------- /inst/templates/delays_report.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Top 5 delays, {{{ report_month }}} {{{ report_year }}}" 3 | output: html_document 4 | --- 5 | 6 | ```{r setup, echo=FALSE} 7 | library(dplyr) 8 | library(ggplot2) 9 | library(here) 10 | 11 | delays <- readRDS(here("reports", "{{{ report_year }}}", "{{{ report_month }}}", "delays_{{{ report_month }}}_{{{ report_year }}}_clean.rds")) 12 | ``` 13 | 14 | ```{r, echo=FALSE} 15 | delays %>% 16 | group_by(line, description) %>% 17 | summarise(delays = sum(min_delay)) %>% 18 | arrange(-delays) %>% 19 | slice(1:5) %>% 20 | ggplot(aes(x = description, 21 | y = delays)) + 22 | geom_col() + 23 | scale_x_discrete("") + 24 | scale_y_continuous("Delay (minutes)") + 25 | ggtitle("Top 10 causes for delay, by line", 26 | subtitle = "{{{ report_month }}} {{{ report_year }}}") + 27 | coord_flip() + 28 | facet_wrap(vars(line), 29 | nrow = 1) + 30 | theme_minimal() 31 | ``` 32 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | # MIT License 2 | 3 | Copyright (c) 2019 Sharla Gelfand 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /R/delays_report.R: -------------------------------------------------------------------------------- 1 | #' Generate template for creating delays report 2 | #' 3 | #' @description Opens a template of the delays report for the month, optionally rendering it. 4 | #' 5 | #' @param report_month Month of report 6 | #' @param report_year Year of report 7 | #' @param render Should the report be rendered automatically? Defaults to `FALSE` 8 | #' 9 | #' @export 10 | delays_report <- function(report_month, report_year, render = FALSE){ 11 | report_path <- fs::path("reports", report_year, report_month) 12 | 13 | usethis::use_template(template = "delays_report.Rmd", 14 | save_as = paste0(report_path, "/03-delays_report.Rmd"), 15 | data = list(report_month = report_month, 16 | report_year = report_year), 17 | package = "delaysreport", 18 | open = !render) 19 | 20 | if(render){ 21 | out_path <- paste0(report_path, "/", report_month, " ", report_year, " ", 22 | "Delays.html") 23 | 24 | rmarkdown::render(input = paste0(report_path, "/03-delays_report.Rmd"), 25 | output_file = paste(report_month, report_year, "Delays.html"), 26 | quiet = TRUE) 27 | 28 | usethis::ui_done("Report saved to {usethis::ui_path(out_path)}") 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /reports/2019/February/02-data_cleaning.R: -------------------------------------------------------------------------------- 1 | # February 2019 TTC delays data cleaning 2 | 3 | library(readxl) 4 | library(dplyr) 5 | library(here) 6 | library(janitor) 7 | library(assertr) 8 | 9 | delays <- read_excel(here("reports", "2019", "February", "delays_February_2019_raw.xlsx")) %>% 10 | clean_names() 11 | 12 | # Check 1: Check that line is one of YU, BD, SHP, SRT -------------------------- 13 | 14 | delays %>% 15 | assert(in_set("YU", "BD", "SHP", "SRT"), line) 16 | 17 | # If it's not, look at the lines that violate this assumption 18 | 19 | delays %>% 20 | filter(!(line %in% c("YU", "BD", "SHP", "SRT"))) %>% 21 | count(line) 22 | 23 | # Recode if necessary -- e.g. Y/U to YU. 24 | # Some may not be captured below, add them as you see fit! 25 | 26 | delays <- delays %>% 27 | mutate(line = case_when(line %in% c("B/D", "BD LINE") ~ "BD", 28 | line == "YUS" ~ "YU", 29 | TRUE ~ line)) 30 | 31 | # Exclude cases where the line still isn't a thing 32 | 33 | delays <- delays %>% 34 | filter(line %in% c("YU", "BD", "SHP", "SRT")) 35 | 36 | # Check 2: All codes have a corresponding description -------------------------- 37 | 38 | # Add descriptions 39 | 40 | delays <- delays %>% 41 | left_join(delaysreport::delay_codes, 42 | by = "code") 43 | 44 | # Check where it's missing 45 | 46 | delays %>% 47 | assert(not_na, description) 48 | 49 | # If any are missing, try to google them and find out what they are. 50 | 51 | delays %>% 52 | filter(is.na(description)) %>% 53 | count(code) 54 | 55 | # No luck? Recode any remaining missing to "Delay Description Unknown" 56 | 57 | delays <- delays %>% 58 | mutate(description = case_when(is.na(description) ~ "Delay Description Unknown", 59 | TRUE ~ description)) 60 | 61 | # Save clean data -------------------------------------------------------------- 62 | 63 | saveRDS(delays, here("reports", "2019", "February", "delays_February_2019_clean.rds")) 64 | 65 | -------------------------------------------------------------------------------- /inst/templates/data_cleaning.R: -------------------------------------------------------------------------------- 1 | # {{{ report_month }}} {{{ report_year }}} TTC delays data cleaning 2 | 3 | library(readxl) 4 | library(dplyr) 5 | library(here) 6 | library(janitor) 7 | library(assertr) 8 | 9 | delays <- read_excel(here("reports", "{{{ report_year }}}", "{{{ report_month }}}", "delays_{{{ report_month }}}_{{{ report_year }}}_raw.xlsx")) %>% 10 | clean_names() 11 | 12 | # Check 1: Check that line is one of YU, BD, SHP, SRT -------------------------- 13 | 14 | delays %>% 15 | assert(in_set("YU", "BD", "SHP", "SRT"), line) 16 | 17 | # If it's not, look at the lines that violate this assumption 18 | 19 | delays %>% 20 | filter(!(line %in% c("YU", "BD", "SHP", "SRT"))) %>% 21 | count(line) 22 | 23 | # Recode if necessary -- e.g. Y/U to YU. 24 | # Some may not be captured below, add them as you see fit! 25 | 26 | delays <- delays %>% 27 | mutate(line = case_when(line %in% c("B/D", "BD LINE") ~ "BD", 28 | line == "YUS" ~ "YU", 29 | TRUE ~ line)) 30 | 31 | # Exclude cases where the line still isn't a thing 32 | 33 | delays <- delays %>% 34 | filter(line %in% c("YU", "BD", "SHP", "SRT")) 35 | 36 | # Check 2: All codes have a corresponding description -------------------------- 37 | 38 | # Add descriptions 39 | 40 | delays <- delays %>% 41 | left_join(delaysreport::delay_codes, 42 | by = "code") 43 | 44 | # Check where it's missing 45 | 46 | delays %>% 47 | assert(not_na, description) 48 | 49 | # If any are missing, try to google them and find out what they are. 50 | 51 | delays %>% 52 | filter(is.na(description)) %>% 53 | count(code) 54 | 55 | # No luck? Recode any remaining missing to "Delay Description Unknown" 56 | 57 | delays <- delays %>% 58 | mutate(description = case_when(is.na(description) ~ "Delay Description Unknown", 59 | TRUE ~ description)) 60 | 61 | # Save clean data -------------------------------------------------------------- 62 | 63 | saveRDS(delays, here("reports", "{{{ report_year }}}", "{{{ report_month }}}", "delays_{{{ report_month }}}_{{{ report_year }}}_clean.rds")) 64 | -------------------------------------------------------------------------------- /.Rhistory: -------------------------------------------------------------------------------- 1 | usethis::use_description(fields = list(Title = "TTC delays report", 2 | `Authors@R` = 'person("Sharla", "Gelfand", email = "sharla.gelfand@gmail.com", role = c("aut", "cre"))', 3 | Description = "Monthly report on top 5 causes for delay for each TTC line.")) 4 | usethis::use_mit_license("Sharla Gelfand") 5 | use_data_raw() 6 | library(usethis) 7 | use_data_raw() 8 | library(dplyr) 9 | library(readxl) 10 | delay_codes <- read_excel("data-raw/Subway & SRT Log Codes.xlsx") 11 | library(janitor) 12 | delay_codes <- read_excel("data-raw/Subway & SRT Log Codes.xlsx") %>% 13 | clean_names() 14 | library(dplyr) 15 | library(readxl) 16 | library(janitor) 17 | delay_codes <- read_excel("data-raw/Subway & SRT Log Codes.xlsx") %>% 18 | clean_names() 19 | delay_codes <- delay_codes %>% 20 | select(code = sub_rmenu_code, description = code_description_3) %>% 21 | remove_empty("rows") %>% 22 | bind_rows( 23 | ttc_delay_codes %>% 24 | select(code = srt_rmenu_code, description = code_description_7) %>% 25 | remove_empty("rows") 26 | ) 27 | delay_codes <- delay_codes %>% 28 | select(code = sub_rmenu_code, description = code_description_3) %>% 29 | remove_empty("rows") %>% 30 | bind_rows( 31 | delay_codes %>% 32 | select(code = srt_rmenu_code, description = code_description_7) %>% 33 | remove_empty("rows") 34 | ) 35 | delay_codes 36 | delay_codes %>% filter(startsWith(cde, "MUN")) 37 | delay_codes %>% filter(startsWith(code, "MUN")) 38 | ?use_data 39 | delay_codes <- delay_codes %>% 40 | bind_rows(missing_codes) 41 | missing_codes <- tribble( 42 | ~code, ~description, 43 | "MUNCA", "No Collector Available - Non E.S.A. Related", 44 | "TRNCA", "No Collector Available", 45 | "PUEO", "Delay Description Unknown" 46 | ) 47 | delay_codes <- delay_codes %>% 48 | bind_rows(missing_codes) 49 | usethis::use_data(delay_codes) 50 | usethis::use_data(delay_codes) 51 | devtools::load_all(".") 52 | fs::dir_tree("/Users/sharla/delaysreport") 53 | fs::dir_tree("/Users/sharla/delaysreport") 54 | library(delaysreport) 55 | fs::dir_tree("/Users/sharla/delaysreport") 56 | ?use_data 57 | library(delaysreport) 58 | delaysreport::delay_codes 59 | usethis::use_r() 60 | usethis::use_r() 61 | usethis::use_r() 62 | usethis::use_r() 63 | ?usethis::use_r 64 | usethis::use_r("setup") 65 | setup <- function(report_month, report_year){ 66 | usethis::use_directory(fs::path("reports", report_year, report_month)) 67 | } 68 | setup("February", "2019") 69 | fs::dir_tree("/Users/sharla/delaysreport") 70 | fs::dir_tree("/Users/sharla/delaysreport") 71 | devtools::load_all(".") 72 | library(delaysreport) 73 | devtools::load_all(".") 74 | library(delaysreport) 75 | rm(setup) 76 | delaysreport::setup("February", 2019) 77 | ?use_template 78 | #' Set up delays report 79 | #' 80 | #' @description Set up delays report, with instructions on how to get the data and helper functions to clean the data and write the report. 81 | #' 82 | #' @param report_month Month of report 83 | #' @param report_year Year of report 84 | #' 85 | #' @export 86 | setup <- function(report_month, report_year){ 87 | report_path <- fs::path("reports", report_year, report_month) 88 | usethis::use_directory(report_path) 89 | usethis::use_template(template = "setup.R", 90 | save_as = paste0(report_path, "/setup.R"), 91 | data = list(report_month = report_month, 92 | report_year = report_year), 93 | package = "delaysreport", 94 | open = TRUE) 95 | } 96 | library(delaysreport) 97 | delaysreport::setup("February", 2019) 98 | delaysreport::setup("February", 2019) 99 | devtools::load_all(".") 100 | library(delaysreport) 101 | rm(list=ls()) 102 | delaysreport::setup("February", 2019) 103 | fs::dir_tree("/Users/sharla/delaysreport") 104 | usethis::use_package("usethis") 105 | devtools::load_all(".") 106 | library(delaysreport) 107 | usethis::use_template(template = "data_cleaning.R", 108 | save_as = "reports/2019/February/data_cleaning.R", 109 | data = list(report_month = "February", 110 | report_year = 2019), 111 | package = "delaysreport") 112 | library(readxl) 113 | library(dplyr) 114 | library(here) 115 | library(janitor) 116 | library(assertr) 117 | delays <- read_excel(here("reports", 2019, February, "delays_February_2019_raw.xlsx")) %>% 118 | clean_names() 119 | delays <- read_excel(here("reports", "2019", "February", "delays_February_2019_raw.xlsx")) %>% 120 | clean_names() 121 | delays <- read_excel(here("reports", "2019", "February", "delays_February_2019_raw.xlsx")) %>% 122 | clean_names() 123 | delays %>% 124 | assert(in_set("YU", "BD", "SHP", "SRT"), line) 125 | delays %>% 126 | filter(!(line %in% c("YU", "BD", "SHP", "SRT"))) %>% 127 | count(line) 128 | delays <- delays %>% 129 | filter(line %in% c("YU", "BD", "SHP", "SRT")) 130 | delays <- delays %>% 131 | left_join(delaysreport::delay_codes, 132 | by = "code") 133 | delays %>% 134 | assert(not_na, description) 135 | delays %>% 136 | filter(is.na(description)) %>% 137 | count(code) 138 | delay_codes %>% filter(startsWith(code, "MU")) 139 | delay_codes %>% filter(startsWith(code, "MUP")) 140 | delays <- delays %>% 141 | mutate(description = case_when(code == "MUPF" ~ "Delay Description Unknown", 142 | TRUE ~ description)) 143 | saveRDS(delays, here("reports", 2019, February, "delays_February_2019_clean.xlsx")) 144 | saveRDS(delays, here("reports", "2019", "February", "delays_February_2019_clean.xlsx")) 145 | usethis::use_r("data_cleaning") 146 | devtools::load_all(".") 147 | library(delaysreport) 148 | usethis::use_rmarkdown_template() 149 | ?usethis::use_rmarkdown_template 150 | template_description = "Monthly report on top 5 causes for delay for each TTC line.) 151 | usethis::use_rmarkdown_template(template_name = "delays_report", 152 | template_description = "Monthly report on top 5 causes for delay for each TTC line.") 153 | fs::dir_tree("/Users/sharla/delaysreport") 154 | devtools::load_all(".") 155 | library(delaysreport) 156 | usethis::use_template("delays_report", open = TRUE) 157 | library(delaysreport) 158 | usethis::use_template("delays_report", open = TRUE) 159 | usethis::use_template("delays_report.Rmd", open = TRUE) 160 | usethis::use_template("delays_report.Rmd", open = TRUE, package = "delaysreport") 161 | usethis::use_r("delays_report") 162 | iris 163 | iris %>% 164 | filter(Species == Species) 165 | library(dplyr) 166 | Species <- "virginica" 167 | iris %>% 168 | filter(Species == Species) 169 | iris %>% 170 | filter(Species == Species) %>% 171 | count(Species) 172 | iris %>% 173 | filter(Species == filter_Species) %>% 174 | count(Species) 175 | filter_Species <- "virginica" 176 | iris %>% 177 | filter(Species == filter_Species) %>% 178 | count(Species) 179 | --------------------------------------------------------------------------------