R/fars_functions.R

Defines functions fars_read make_filename fars_read_years fars_summarize_years fars_map_state

Documented in fars_map_state fars_read fars_read_years fars_summarize_years make_filename

#' Read a FARS file
#'
#' This function reads a file, to be precise a  file from Fatality Analysis Reporting System.
#' We can pass a file name to read (using the \code{filename} argument).
#'
#' @param filename A character.
#'
#' @return This function returns a dataframe table.
#'
#' @note This function will throws an error if the filename doesn't exist.
#'
#' @examples
#' \dontrun{
#' 	fars_read("fars_data.csv")
#' 	fars_read(filename = "fars_data.csv")
#' 	fars_read("data/accident_2013.csv")
#' }
#'
#'
#' @importFrom readr read_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)
}
#'
#' Make a file name for the given year.
#'
#' This function creates a file name in desired pattern by taking a \code{year} as input.
#'
#' @param year A character.
#'
#' @return This function will return the file name in desired pattern for the given \code{year}.
#'
#' @examples
#' \dontrun{
#'  	make_filename("2013")
#' 	make_filename(year = "2014")
#' }
#'
#' @export
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}
#'
#' Data for the given years.
#'
#' This function takes a vector of years, and for each year in the vector it uses \code{make_filename(year)} to make a file name pattern
#' for the given year and \code{fars_read(filename)} to read the file.
#'
#' @param years A vector of years.
#'
#' @return A list of dataframes for each year in the vector of years if that year exist,
#' or it will return a NULL for invalid year.
#'
#' @note This function will throw a warning if the entered year is invalid.
#'
#' @examples
#' \dontrun{
#'  fars_read_years(c("2013", "2014"))
#'  yearvector <- c("2013", "2014")
#'  fars_read_years(yearvector)
#' }
#'
#' @importFrom dplyr mutate select
#'
#' @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 data by years.
#'
#' This function takes a vector of \code{years} as input. Then reads the list generated by \code{fars_read_years(years)} by calling it.
#' Then summarize the data and spreads the data in multiple column.
#'
#' @inheritParams fars_read_years
#'
#' @return summarized data that spread across multiple columns.
#'
#' @examples
#' \dontrun{
#'	fars_summarize_years(c("2013", "2014"))
#'	fars_summarize_years(yearvector)
#' }
#'
#' @importFrom dplyr bind_rows group_by summarize
#' @importFrom tidyr spread
#'
#' @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 with plotted accidents.
#'
#' This function will take \code{state.num} and \code{year} as input and returns a map with accident plotted in it.
#' It will throw an error if the \code{state.num} is an invalid'.
#'
#' @param state.num A character that represents the state number.
#' @inheritParams make_filename
#'
#' @return a map with accidents plotted in it. It will also returns a message that there are no accidents to plot
#' if there no records of accident for the given state.
#'
#' @note This function will throws an error if \code{state.num} is invalid.
#'
#' @examples
#' \dontrun{
#'	fars_map_state("1", "2013")
#' 	fars_map_state(state.num = "1", year = "2013")
#'  }
#'
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @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)
  })
}
GopalSeshadri/MyFirstPackage documentation built on May 6, 2019, 6:30 p.m.