R/fars_functions.R

Defines functions fars_map_state fars_summarize_years fars_read_years make_filename fars_read

Documented in fars_map_state fars_read fars_read_years fars_summarize_years make_filename

if(getRversion() >= "2.15.1")  utils::globalVariables(c("STATE","MONTH", "year", "n"))

#' Accidents records from FARS for year 2013.
#'
#' Data with the accidents records from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' for the year of 2013
#'
#' @format A data frame with 30202 rows and 50 variables
"accident_2013"

#' Read FARS csv data
#'
#' Reads the data from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' This is a helper function for \code{\link{fars_read_years}}
#'
#'
#' @param filename the name of the file generated from the FARS
#'
#' @return A tibble with the read data
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
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)
}


#' Generate FARS csv data filenames
#'
#' Generate csv data filenames from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' This is a helper function for \code{\link{fars_read_years}}
#'
#' @param year The year of interest
#'
#' @return the corresponding FARS filename to the year provided
#'
make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("accident_%d.csv.bz2", year)
}

#' Read FARS csv data from provided years
#'
#' Reads the data from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' from the provided years
#'
#' @param years A vector of years of interest
#'
#' @return A list of tibbles with the read data from the provided years
#'
#' @importFrom dplyr mutate select
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 from provided years
#'
#' Reads the data from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' from the provided years and generate a summary
#'
#' @param years A vector of years of interest
#'
#' @return A tibbles with the summart from the provided years
#'
#' @examples
#' con <- bzfile("accident_2013.csv.bz2", "w")
#' write.csv(accident_2013, con, row.names = FALSE)
#' close(con)
#'
#' fars_summarize_years(2013)
#'
#' ## This will generate an error because the year 2016 is not avaliable
#' \dontrun{fars_summarize_years(2016))}
#' @export
#'
#' @importFrom dplyr bind_rows group_by summarize %>%
#' @importFrom tidyr spread
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)
}

#' Plot a map from FARS data
#'
#' Reads the data from the US National Highway Traffic Safety Administration's
#' \href{http://www.nhtsa.gov/Data/Fatality-Analysis-Reporting-System-(FARS)}{Fatality Analysis Reporting System}
#' from the provide year and state and plot a map with the accidents
#'
#' @param state.num The numer code of the state of interest, from 1 to 56.
#' @param year The year of interest
#'
#' @return None
#'
#' @examples
#' con <- bzfile("accident_2013.csv.bz2", "w")
#' write.csv(accident_2013, con, row.names = FALSE)
#' close(con)
#'
#' fars_map_state(23, 2013)
#'
#' ## This will generate an error because the year 2016 is not avaliable
#' ## and because there is no state 58
#' \dontrun{fars_map_state(58, 2016))}
#' @export
#'
#' @importFrom dplyr filter
#' @importFrom graphics points
#' @import maps
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)
        })
}
fernandovmacedo/samplePackage documentation built on May 14, 2017, 1:14 a.m.