#' @title Read CSV File
#'
#'@description Read a csv file and convert it into a data frame
#'
#' @param filename A csv file
#'
#' @return A data frame
#' @export
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#'
#' @note The filename must take into consideration the current working directory
#'
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)
}
#' @title Create Filename
#'
#' @description Compose the name of the accidents file for a specific year
#' @name make_filename
#' @param year The year of the accident
#'
#' @return The output of \code{\link{sprintf}}
#' @export
#'
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' @title Read Multiple CSV Files
#'
#' @description Read multiple csv accident files
#'
#' @name fars_read_years
#'
#' @param years A vector of the desired years to check
#'
#' @return A data frame of months and year for each year
#' @export
#'
#' @importFrom dplyr mutate select
#' @importFrom magrittr %>%
#'
#' @note The function will throw an error if an invalid year is entered
#'
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)
})
})
}
#' @title Summarize Accidents
#'
#' @description Summarize the number of accidents each month
#'
#' @name fars_summarize_years
#'
#' @param years A vector of the desired years to check
#'
#' @return A data frame with the number of accidents every month for each year
#' @export
#'
#' @importFrom dplyr bind_rows group_by summarize
#' @importFrom tidyr spread
#' @importFrom magrittr %>%
#'
#' @note The function will throw an error if an invalid year is entered
#'
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)
}
#' @title Plot accidents
#'
#' @description Creates a plot of the accidents in a specified state and year
#'
#' @name fars_map_state
#'
#' @param state.num The number of the state
#' @param year The desired year
#'
#' @return A plot of the accidents in a specified state and year
#' @export
#'
#' @importFrom dplyr filter
#' @importFrom magrittr %>%
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @note The function will throw an error if the number of state is invalid
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.