#' Read data from a comma separated value (CSV) file
#'
#' Note:
#' - file is usually specified by make_filename function
#' - read_csv function from library readr is used
#' - file can be in zipped format
#' -if file doesn't exist an error is thrown
#'
#' @param filename character string, may include paths
#'
#' @return this function returns a tibble (dplyr::tbl_df)
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#'
#' @examples
#' \dontrun{
#' data2015 <- fars_read("v:/r/data/accident_2015.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 standard filename for the specified year
#' stadard filename format = "accident0000.csv.bz2" (0000 = year)
#'
#' @param year integer value, specifying the year
#'
#' @return this function returns an character string containg the standard filename
#'
#' @examples
#' \dontrun{
#' makefilename(2015)
#' }
#'
#' @export
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' Create tables for months of accidents in the years specified
#'
#'
#' @param years numeric/integer vector/list specifying the years to be read
#'
#' @return a list of tibbles containing the months and year of incidents in the
#' specified years. If the year/file doesn't exist a warning is generated and
#' NULL is returned
#'
#' @importFrom dplyr mutate, select, %>%
#'
#' @examples
#' \dontrun{
#' data3years <- fars_read_years(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)
})
})
}
#' Create summary of the number of accidents per month in the years specified
#'
#' @param years numeric/integer vector/list specifying the years to be summarized
#'
#' @return a table (tibble) of the incidents per month (rows) per year (columns)
#'
#' @importFrom dplyr bind_rows group_by summarize %>%
#' @importFrom tidyr spread
#'
#' @examples
#' \dontrun{
#' fars_summarize_years(2013: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)
}
#' Creates a map of the accidents in a year in a US State
#' The latitude and longitude are extracted from the file
#' for this.
#'
#' @param state.num number of the US State (numeric/integer)
#' @param year year of the accidents in the analysis (numeric/integer)
#'
#' @return generates a plot of the accidents in the specified year
#' and US State. Errors are returned if the year is invalid (no file)
#' or state number is invalid (or not present in the data)
#'
#' @importFrom dplyr filter
#' @importFrom maps map
#'
#' @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.