# Oscar Arturo Bringas
# Coursera "Building R Packages"
# Week 2 Assigment -- Documenting Functions
# April 30 2017
#' Reads a FARS file.
#'
#' This function creates a tbl_df object from the data reading of the
#' US National Highway Traffic Safety Administration's Fatality Analysis
#' Reporting System (FARS) once the file name has been given.
#'
#' @param filename A string of characters with the name of the FARS file to be
#' read.
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#' @return A tbl_df object containing the FARS data. If the file name does not
#' exist, the function will stop.
#'
#' @examples
#' \dontrun{
#' fars_read(filename = "accident_2014.csv.bz2")
#' }
#'
#' @export
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)
}
#' Create a FARS filename.
#'
#' This function creates a character string with a valid file name for reading
#' the FARS data given a year of interest.
#'
#' @param year An integer or string with the year of interest.
#' @return A string of characters with the appropriate format for reading FARS
#' data of the year of interest.
#'
#' @examples
#' \dontrun{
#' make_filename(year = 2013)
#' make_filename(year = "2014")
#' }
#'
#' @export
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' Read multiple FARS files.
#'
#' This function receives a vector of years and creates a tbl_df object that
#' contains the FARS data for each year. An error message will appear if for
#' any given year there is no file name.
#'
#' @param years A vector containing a set of years.
#' @importFrom magrittr %>%
#' @importFrom dplyr mutate select
#' @return Return a list. For each year introduced it creates the appropriate format for
#' reading and loading FARS files, adds the "year" column and selects only the
#' "month and year" columns. If data do not exist for a given year, send a
#' warning and return NULL value.
#'
#' @examples
#' \dontrun{
#' fars_read_years(years = 2013)
#' fars_read_years(years = c("2013","2015"))
#' }
#'
#' @export
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)
})
})
}
#' Summarize FARS data
#'
#' This function summarizes the number of records that are for each month and
#' each year of the requested information per year.
#'
#' @param years A vector of years.
#'
#' @importFrom dplyr bind_rows group_by summarize
#' @importFrom tidyr spread
#'
#' @return A table with the summary information of the number of registrations
#' per month of each year requested.
#'
#' @examples
#' \dontrun{
#' fars_summarize_years(2013)
#' fars_summarize_years(c(2014,2015))
#' fars_summarize_years(c("2013","2014","2015"))
#' }
#'
#' @export
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)
}
#' Mapping locations of fatal accidents
#'
#' This function maps each location where a fatal accident occurred for each
#' state and year provided. The maps library is necessary.
#'
#' @param state.num A numeric code for US state.
#' @param year A numeric or string of the year of interest.
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#' @return A map of the United States mapping where a fatal accident
#' occurred in the state and year of interest. An error occurs if there are
#' no accidents registered in the state or if the year or the status key are
#' not valid.
#'
#' @examples
#' \dontrun{
#' fars_map_state(5,2013)
#' }
#'
#' @export
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)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.