R/fars_functions.R

#' Read fars file
#' @details This function read file and transform it to tibble format
#' @section Warning:
#' If file does not exist, function throws exception.
#' @param filename name of csv file for read
#' @return tibble format of csv file
#' @examples
#' \dontrun{
#' data.tbl <- fars_read(filename="data/accident_2013.csv.bz2")
#' }
#' @import dplyr
#' @import readr
#' @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 file name using year
#' This function make a filename from year which is provided as paramter
#' @param year Year of dataset data
#' @return name of the dataset
#' @examples
#' \dontrun{
#' file.name <- make_filename(2013)
#' }
#' @export
make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("data/accident_%d.csv.bz2", year)
}

#' Read datasets from years
#'
#' Read each dataset with years which are defined as parameter and transform data to the tibble format. Also, tibble consists
#' of year and month features.
#'
#' @param years Array which contains years of the dataset
#'
#' @section Warning:
#' If dataset does not exist with year parameter, function writes a warning and return NULL
#' @return Array of tibble
#' @examples
#' \dontrun{
#' datasets <- fars_read_years(c(2013, 2014))
#' }
#' @export
#' @import dplyr
fars_read_years <- function(years) {
        lapply(years, function(year) {
                file <- make_filename(year)
                tryCatch({
                        dat <- fars_read(file)
                        return(dplyr::mutate(dat, year = year) %>%
                                dplyr::select(MONTH, year))
                }, error = function(e) {
                        warning("invalid year: ", year)
                        return(NULL)
                })
        })
}

#' Get year, n key-value
#'
#' Function binds datasets and summarizes data by year and month.
#'
#' @inheritParams fars_read_years
#' @return tibble which contains year and count of elements key-value pairs
#' @examples
#' \dontrun{
#' years.n.value.pairs <- fars_summarize_years(c(2013, 2014))
#' }
#' @export
#' @import dplyr
#' @import tidyr
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)
}

#' Draw points on the map
#'
#' This function draws points on the map according to the dataset which depends on year parameter.
#' Also, function filters data with state.num
#'
#' @section Warnings:
#' If state does not exist, function throws error: invalid STATE number
#' If data rows are 0 after filtering, then function returns NULL and warning.
#' @param state.num number of the state
#' @param year Year of the dataset
#' @return map plot with points
#' @export
#' @import maps
#' @import dplyr
#' @import graphics
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)
        })
}
Saphirox/Week4 documentation built on June 7, 2019, 9:53 a.m.