knitr::opts_chunk$set(echo = TRUE)
The "fars_read" function reads a CSV file, given a file name, without showing a progress line. Throws an error line if the file doesn't exist. Name of the file is the only input parameter and it returns a tibble.
fars_read <- function(filename) { if(!file.exists(filename)) stop("file '", filename, "' does not exist") data <- suppressMessages({ readr::read_csv(filename, progress = FALSE) }) dplyr::tbl_df(data) }
Example: fars_read("../data/accident_2013.csv.bz2")
fars_read("C:/Workstation/coursera/Mastering_R/final_assignment_building_R_package/package/farsFinal/data/accident_2013.csv.bz2")
The "make_filename" function creats a file name based on the year that is provided as a parameter. It returns a string, which is the file name with bz2 zipped extension.
make_filename <- function(year) { year <- as.integer(year) sprintf("C:/Workstation/coursera/Mastering_R/final_assignment_building_R_package/package/farsFinal/data/accident_%d.csv.bz2", year) }
Example: make_filename(2013)
make_filename(2013)
The "fars_read_years" function reads a range of valid years, calls the make_filename function and reads data in batch mode. It returns a list of tibble objects given the range of valid years. If a valid year is not given, the function throws an error message.
require(magrittr) fars_read_years <- function(years) { lapply(years, function(year) { file <- make_filename(year) tryCatch({ dat <- fars_read(file) dplyr::mutate(dat, year = year) %>% dplyr::select(MONTH, year) }, error = function(e) { warning("invalid year: ", year) return(NULL) }) }) }
Example: fars_read_years(2013:2014)
fars_read_years(2013:2014)
The "fars_summarize_years" function counts the occurance by years and months, calls the "fars_read_years" function to do so. It takes a range of integers (years) as input and returns a wide format tibble object, given the range of valid years, of the count of occurance by years and months.
library(magrittr) fars_summarize_years <- function(years) { dat_list <- fars_read_years(years) dplyr::bind_rows(dat_list) %>% dplyr::group_by(year, MONTH) %>% dplyr::summarize(n = n()) %>% tidyr::spread(year, n) }
Example: fars_summarize_years(2013:2014)
fars_summarize_years(2013:2015)
The "fars_map_state" function draws a map for a given state number in the data and a valid year. It returns a map. It throws an error "invalid STATE number:" if an invalid state number is given. If no data is available, it throws a message "no accidents to plot".
fars_map_state <- function(state.num, year) { filename <- make_filename(year) data <- fars_read(filename) state.num <- as.integer(state.num) if(!(state.num %in% unique(data$STATE))) stop("invalid STATE number: ", state.num) data.sub <- dplyr::filter(data, STATE == state.num) if(nrow(data.sub) == 0L) { message("no accidents to plot") return(invisible(NULL)) } is.na(data.sub$LONGITUD) <- data.sub$LONGITUD > 900 is.na(data.sub$LATITUDE) <- data.sub$LATITUDE > 90 with(data.sub, { maps::map("state", ylim = range(LATITUDE, na.rm = TRUE), xlim = range(LONGITUD, na.rm = TRUE)) graphics::points(LONGITUD, LATITUDE, pch = 46) }) }
Examples fars_map_state(1, 2013)
fars_map_state(1, 2013)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.