#' FARS: A package for computating the notorious bar statistic.
#'
#' This package contains R functions which provide informatio about fatal injuries in car accidents in the US.
#' The data is from the \strong{US National Highway Traffic Safety Administration's} \emph{Fatality Analysis
#' Reporting System} (FARS), which is a nationwide census, providing the
#' American public yearly data, regarding fatal injuries suffered in motor
#' vehicle traffic crashes. For more information, see:
#' \itemize{
#' \item{\url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}}
#' \item{\url{https://en.wikipedia.org/wiki/Fatality_Analysis_Reporting_System}}
#' }
#'
#' @docType package
#' @name FARS
NULL
#' Read file with FARS data
#'
#' This helper function (not exported) reads FARS data from .csv file, stored in the
#' current working directory. Each file contains data about fatal injuries in MV accidents in one year.
#'
#' @details For more information on the data, see:
#' \itemize{
#' \item{\url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}}
#' }
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#'
#' @param filename A character string with the name of the file to read, see
#' notes.
#'
#' @return A data frame (tbl_df) with data read from the csv file, or an error if the
#' file does not exists.
#'
#' @examples
#' library(magrittr)
#'
#' year <- 2015
#' data_2015 <- FARS:::fars_read(FARS:::make_filename(year))
#' head(data_2015)
#'
#' @note Make filename with: \code{\link{make_filename}}
#'
#' @seealso \link{make_filename}
#'
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)
}
#' Make name of file with FARS data
#'
#' Make .csv file name for file with data from \code{year}
#'
#' @details
#' \itemize{
#' \item{The function does not check if the file is available.}
#' }
#'
#' @param year A string or an number specifying the year in which the desired data is collected.
#'
#' @return This function returns a string with name of the file containing the data for the given
#' year.
#'
#' @examples
#' FARS:::make_filename(2013)
#'
#'
# not exported
make_filename <- function(year) {
year <- as.integer(year)
system.file("extdata",
sprintf("accident_%d.csv.bz2", year),
package = "FARS",
mustWork = FALSE)
}
#' Read FARS years
#'
#' Ancillary function used by \code{fars_summarize_years}. Reads the FARS data of each
#' specified year and returns the year and month of each fatal injury
#'
#' @param years A vector or list with one or more years. Valid years are 2013, 2014 and 2015.
#'
#' @importFrom dplyr mutate_
#' @importFrom dplyr select_
#' @importFrom magrittr "%>%"
#
#' @return A data frame (tbl_df) with variables year and month. Each row represents a fatal injury
#' month. Returns NULL if a year in \code{years} is not valid.
#'
#' @seealso \link{fars_summarize_years}
#'
#' @examples
#' FARS:::fars_read_years(2013)
#'
# not exported
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)
})
})
}
#' Summarise FARS data by years
#'
#' This function gives the number of fatal in injuries in car accidents by year and month. The data is from
#' the \strong{US National Highway Traffic Safety Administration's} \emph{Fatality Analysis
#' Reporting System} (FARS), which is a nationwide census, providing the
#' American public yearly data, regarding fatal injuries suffered in motor
#' vehicle traffic crashes. For more information, see:
#' \itemize{
#' \item{\url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}}
#' \item{\url{https://en.wikipedia.org/wiki/Fatality_Analysis_Reporting_System}}
#' }
#'
#' @param years A vector or list of years to summarize by.
#'
#' @return A data frame (tbl_df) with number of accidents by years summarized by month.
#'
#' @details Gives warnings if one or more, but not all years are invalid; throws an error
#' when all years are invalid
#'
#' @importFrom dplyr bind_rows
#' @importFrom dplyr group_by_
#' @importFrom dplyr summarize_
#' @importFrom tidyr spread_
#' @importFrom magrittr "%>%"
#'
#' @examples
#'
#' fars_summarize_years(2013)
#' fars_summarize_years(2013:2015)
#'
#' # gives warning
#' fars_summarize_years(2012:2013)
#'
#' # throws an error
#' year = 2010
#' tryCatch({fars_summarize_years(year)},
#' error = function(e) { warning("invalid year: ", year)
#' return(NULL)})
#'
#' @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)
}
#' Display map of accidents with fatal injuries by state and year
#'
#' The data is from the \strong{US National Highway Traffic Safety Administration's} \emph{Fatality Analysis
#' Reporting System} (FARS), which is a nationwide census, providing the
#' American public yearly data, regarding fatal injuries suffered in motor
#' vehicle traffic crashes. For more information, see:
#' \itemize{
#' \item{\url{https://www.nhtsa.gov/research-data/fatality-analysis-reporting-system-fars}}
#' }
#'
#' @param state.num An integer or string indicating the state
#' @param year A string, or an integer
#'
#' @details If the \code{state.num} or \code{year} is invalid the function throws an error.
#'
#' @importFrom maps map
#' @importFrom dplyr filter_
#' @importFrom graphics points
#'
#' @return None
#'
#' @examples
#' \dontrun{
#' fars_map_state(49, 2015)
#' }
#' @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.