#' Read a fars_read file
#'
#'This is a simple function that imports data from csv files
#'
#' @param filename The name of the file to be opened.
#'
#' @return This function returns a tibble of the specified data.
#' An error will occur if the specified data is not found.
#'
#' @importFrom dplyr tbl_df
#' @importFrom readr read_csv
#'
#' @examples
#' \dontrun{
#' fars_read("accident_2013.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 filename from fars data
#'
#' This function creates a filename. from on a chosen year.
#'
#' @param year A numeric or character year to return in the filename.
#'
#' @return The function returns a character string of the filename.
#'
#' @examples
#' make_filename(2015)
#'
#' @export
make_filename <- function(year) {
year <- as.integer(year)
system.file("extdata", sprintf("accident_%d.csv.bz2", year), package="fars")
}
#' Create list of tibbles with yearly and monthly observations
#'
#' This function create a list for each specified year containing a tibble with month and year columns referred to a fars_read file.
#' It also check if there is data for the selected year.
#'
#' @param years A vector of years from which create the lists.
#'
#' @return The function returns a list for each year containing the columns of the month and year.
#'
#' @importFrom dplyr mutate select tbl_df
#' @importFrom readr read_csv
#'
#' @examples
#' \dontrun{
#' years <- c(2013,2014,2015)
#' fars_read_years(years)
#' }
#'
#' @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)
})
})
}
#' Summarize the number of accidents for each year and month
#'
#' This function summarizes the number of accidents for each month in the selected years.
#' @param years A vector of years from which summarize the monthly number of observations.
#'
#' @return The function return a tibble summarizing the number of observations
#' for each month in the selected years.
#'
#' @import dplyr
#' @importFrom readr read_csv
#' @importFrom tidyr spread
#'
#' @examples
#' \dontrun{
#' library(dplyr)
#' years <- c(2013,2014)
#' fars_summarize_years(years)
#' }
#'
#' @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 accident data
#'
#'The function plot a the map of incidents from a specified US State in a specified year.
#'
#' @param state.num ID number of a US State.
#' @param year The year to be mapped.
#'
#' @return The function returns a map with incidents' coordinates or NULL in case of no accidents.
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @examples
#'
#' fars_map_state(1,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.