R/fars_functions.R

#' Return a tibble with two columns : a year and months
#' of dataset for specified year
#'
#'This function loops over inputed list of years and returns
#'a tibble with two columns : a year and months for specified year
#'of an existing dataset, if no dataset exist a NULL is returned with warning
#'
#'@param years A list that specifies the years
#'
#'@return A tibble of month and year columns,
#'in case there is no dataset corresponding to specified year a NULL
#'will be returned with a warning.
#'
#' @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)
    })
  })
}

#' make_filename: Generate the filename
#'
#' Make a .csv data dile name regarding the given \code{year}
#' @param year A string or an integer \code{year}
#' @return This function returns a string with the data file name for a given
#'   year, and the file path within the package.
#' @examples
#' make_filename(2013)
#' @seealso \link{fars_read}
#' @export
make_filename <- function(year) {
  year <- as.integer(year)
  sprintf("accident_%d.csv.bz2", year)
}




#' Get frequency counts of each month in the datasets that correspond
#' to years list
#'
#' This function returns a tibble with a column for each year that has a
#' dataset corresponding to that year. The column specifies a frequency count
#' of month rows in this dataset.
#'
#' @param years A list that specifies the years
#'
#' @return A tibble of month and years columns,
#'in case there is no dataset corresponding to specified year a NULL
#'will be returned with a warning
#'
#' @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)
}


#' Plot points of accidents on state map
#'
#' This function takes a state number and a year and presents a map
#' of the state with plotted accident coordinates. In case of invalid
#' state num an error with a message is returned.
#'
#' @param state.num A character string that specifies the state number
#' @param year A character string that specifies the year
#'
#' @return NULL, side effect is plotting a state map with accidents
#'
#' @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)
  })
}
MichaelGurkov/FinalProjectFars documentation built on May 13, 2019, 1:21 p.m.