#' Data Input as Data Frame
#'
#' Reads a file in .csv format and creates a tibble data frame.
#'
#' @param filename A character string ending in .csv giving the file name
#' of the data to be read into R as a data frame
#'
#' @return This function returns a dataset as a tibble data frame. As a side
#' effect, this function prints the data frame, unless coerced as an object.
#'
#' @details If \code{filename} does not match an existing file, function will return
#' an error: \code{"file filename does not exist"}.
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#'
#' @examples \dontrun{fars_read(accident_2013.csv)}
#' @examples \dontrun{fars_read(accident_2014.csv)}
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 File Name
#'
#' Creates a file named "accident_YEAR.csv.bz2". YEAR in the file name can be
#' specified with the \code{year} argument.
#'
#' @param year An integer or character string giving the year for the filename
#'
#' @return This function returns a file name. As a side effect, this function
#' prints the file name.
#'
#' @examples \dontrun{make_filename(2013)}
#' @examples \dontrun{make_filename(2014)}
#'
#' @export
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' Select \code{MONTH} from Single Year Dataset
#'
#' Creates a tribble data frame containing \code{MONTH} and \code{year} of a
#' specified year's (specified with the \code{year} argument) dataset.
#'
#' @param years An integer or character string giving the year for the filename
#'
#' @return This function returns tribble data frame with column names of
#' \code{MONTH} and \code{year}. \code{MONTH} are integers ranging from
#' 1-12 indicating January to December. As a side effect, this function
#' returns the data frame, unless coerced as an object.
#'
#' @details If \code{year} is not contained within the datasets (2013, 2014, or 2015),
#' the function will return a warning: \code{"invalid year:"}.
#'
#' @importFrom dplyr mutate select
#'
#' @examples \dontrun{fars_read_years(2013)}
#' @examples \dontrun{fars_read_years(2014)}
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)
})
})
}
#' Count of Accidents for each \code{MONTH} from Single Year Dataset
#'
#' Creates a tribble data frame containing \code{MONTH} and count of observations
#' per month in a year's (specified with the \code{years} argument) dataset.
#'
#' @param years An integer or character string giving the year for the filename
#'
#' @return This function returns tribble data frame with column names of \code{MONTH}
#' and specified \code{years}. \code{MONTH} are integers ranging from 1-12
#' indicating January to December. The specified \code{years} column are integers
#' representing the number of observations in each month. As a side effect, this
#' function returns the data frame, unless coerced as an object.
#'
#' @details If \code{year} is not contained within the datasets (2013, 2014, or 2015),
#' the function will return a warning: \code{"invalid year:"}.
#'
#' @importFrom dplyr bind_rows group_by summarize
#' @importFrom tidyr spread
#'
#' @examples \dontrun{fars_summarize_years(2013)}
#' @examples \dontrun{fars_summarize_years(2014)}
#'
#' @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)
}
#' Map of Accidents within a State and Year
#'
#' This function creates a map of accident locations within the specified state
#' (\code{state.num}) and \code{year}.
#'
#' This function requires both the \code{maps} and
#' \code{graphics} packages.
#'
#' @param state.num An integer corresponding to a specific state. Indicates which
#' state to map
#' @inheritParams make_filename
#'
#' @return This function returns a graphic plot of the accident locations within
#' a specified state (\code{state.num}) and \code{year}. Points indicate accident
#' locations and solid lines indicate state boundary.
#'
#' @details If \code{state.num} is not within STATE column (1-50 corresponding to 50 states
#' in alphabetical order), will return error: \code{"invalid STATE number:"}.
#' @details If there are no accidents within the specific \code{state.num} and \code{year},
#' function will return message: \code{"no accidents to plot"}.
#' @details If \code{year} is not contained within the datasets (2013, 2014, or 2015),
#' the function will return a warning: \code{"invalid year:"}.
#'
#' @importFrom dplyr tbl_df
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @examples \dontrun{fars_map_state(1,2013)}
#' @examples \dontrun{fars_map_state(20,2014)}
#'
#' @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.