R/exam_functions.R

#----------------------------------------------------------------

#' Load a csv file into a tbl data.frame.
#'
#' @param filename A character.
#'
#' @return A tbl data.frame.
#'
#' @importFrom dplyr tbl_df
#' @importFrom readr read_csv
#'
#' @note
#' Will throw an error if file does not exist.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' fars_read("mydata.csv")
#' fars_read("C:\\mydata.txt")
#' }
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)
}

#----------------------------------------------------------------

#' Generate a "accident_YEAR.csv.bz2" string with a specified YEAR.
#'
#' @param year An integer or a character.
#'
#' @return A character string.
#'
#' @note
#' Will throw a warning and malfunction if year can't be
#' converted to an integer (eg. "HELLO" will produce
#' "accident_NA.csv.bz2").
#'
#' @export
#'
#' @examples
#' \dontrun{
#' make_filename(2014)
#' make_filename("2018")
#' }
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}

#----------------------------------------------------------------

#' Load several accident_YEAR.csv.bz2 files into a list of tbl data.frame.
#'
#' @param years A vector of years (numeric or character).
#'
#' @return A list in which each element corresponds to a loaded file.
#'
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @import magrittr
#'
#' @note
#' Will throw error and not function if any element inside years
#' isn't related to an existing filename.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' fars_read_years(2013, 2014, 2015)
#' fars_read_years("2013")
#' }
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)
    })
  })
}

#----------------------------------------------------------------

#' Count the number of accidents for each month of every specified years
#'
#' @param years A vector of years (numeric or character).
#'
#' @return A tbl data.frame containing the summary (months in rows, years in columns)
#'
#' @importFrom dplyr bind_rows
#' @importFrom dplyr group_by
#' @importFrom dplyr summarize
#' @importFrom tidyr spread
#' @import magrittr
#'
#' @note
#' Will throw error and not function if any element inside years
#' isn't related to an existing filename.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' fars_summarize_years(c(2013, 2014, 2015))
#' fars_summarize_years("2013")
#' }
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)
}

#----------------------------------------------------------------

#' Plot all accidents recorded a specified year in a state as black dots on a map
#'
#' @param state.num A state number as integer or character.
#' @param year A year as integer or character.
#'
#' @return A plot.
#'
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @note
#' Will throw error and not function if input state number
#' has no data points, if the year isn't related to a filename
#' or if several years / state.nums are set as inputs.
#'
#' @export
#'
#' @examples
#' \dontrun{
#' fars_map_state(13, 2013)
#' fars_map_state("4", "2015")
#' }
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)
  })
}
KDallaporta/TestPackage documentation built on May 14, 2019, 10:32 a.m.