library(roxygen2)
library(tidyverse)
#' Read a CSV format file into a data frame.
#'
#' The function determines if \code{filename} argument) exists and stops grecefully
#' if it does not. If \code{filename} exists than the contents are returned as a
#' data frame. If \code{filename} does not exist then nothing is returned, or assigned, but
#' no error or warning is generated.
#'
#' @param filename A character string with the name of a file. Relative
#' or absolute paths may be used to locate the file in the string.
#'
#' @return This function returns a data frame constructed from the file contents.
#'
#' @import dplyr
#'
#' @examples
#'
#'\dontrun{
#'#' fars_read(inputfile)
#' fars_read("another.csv")
#'}
#'
#' @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)
}
#' Generates a file name based on a year and standard pattern.
#'
#' The function takes the argument \code{year} and casts it to an integer before inserting it within
#' the pattern \code{accident<year>.csv.bz2} and returning the result. If \code{year} cannot be cast
#' to an integer then 'NA' is placed in the file name for the integer year.
#'
#' @param year An integer value repersenting the year.
#'
#' @return This function returns a text string file name constructed from the the integer
#' year embedded into a pattern.
#'
#' @examples
#'\dontrun{
#'#' yearfile <- make_filename(current_year)
#' yearfile2 <- make_filename(2010)
#' yearfile2 <- make_filename("2010")
#'}
#' @export
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' Reads a sequence of years
#'
#' The function an argument \code{years} and builds a data fileneame out of it using \code{make_filename}
#' Uses \code{dplyr::mutate} and \code{dplyr::select}. A list with the same number of elements as \code{years}
#' is returned with the contents of the respective file in each element. If a file does not exist with the
#' name generated by \code{make_filename} then a warning is issued and that position in the returned list is
#' NULL.
#'
#' @param years An list of interger values (or values that will cast to integer) repersenting years.
#'
#' @return This function returns a list of tibbles constructed from the data contained in the file for
#' each year.
#'
#' @importFrom dplyr mutate select
#'
#' @examples
#'\dontrun{#' fars_read_years(c(2013,2015)
#' fars_read_years(2014)
#'}
#' @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)
})
})
}
#' Generates a table (tibble) summarizing the number of accidents by year and month.
#'
#' The function uses the argument \code{years} and returns a summary of accidents for \code{years}.
#'
#'@param years An list of interger values (or values that will cast to integer) repersenting years.
#'
#' @return This function returns an (n+1) x 12 tibble where n is the number of elements in the
#' argument \code{years}. The additional column is the month number. The value of each cell is
#' the count of accidents by month (row) and year (column).
#'
#' @import dplyr
#'
#' @examples
#'\dontrun{#' fars_summarize_years(c(2013,2015)
#' 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)
}
#' Generates a map of the state outline and location of accidents.
#'
#' The function checks the argument \code{state.num} and returns gracefully if the integer is outside
#' the range of state codes. NA values are treated for the location of the accident (latitude and longitude)
#' before generating the plot.
#'
#' @param state.num integer ID for state
#' @param year an integer value or a value that can be ast to an integer.
#'
#' @return This function returns a map object.
#'
#' @import dplyr
#' @import tidyr
#' @importFrom maps map
#'
#' @examples
#'\dontrun{
#'#' fars_map_state(1, 2014)
#' fars_map_state(40, 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.