R/fars_functions.R

globalVariables(c("STATE", "MONTH", "year"))
#' Import a file given its name
#'
#' Given a file name, this function imports a file in CSV format
#' and converts it in a object with the classes "tbl_df", "tbl" and
#' "data.frame". Otherwise, it stops the execution an returns an error
#' message.
#'
#' @param filename a charcater string specifying the file name
#'
#' @return This function returns an object with the classes "tbl_df",
#' "tbl" and "data.frame".If the file doesn't exist in the working
#' directory, it will stop the execution and return an error message
#'
#' @importFrom readr read_csv
#' @import dplyr
#'
#' @examples \dontrun{
#' data_2013 <- fars_read("accident_2013.csv.bz2")
#' data_2013}
#'
#' @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)
}

#' Generate a file name given a year
#'
#' Given a year (a number), this function generates a character
#' string (a filename) with the coding "accident_year.csv.bz2".
#'
#' @param year a numeric representing the year
#'
#' @return a character string with the coding "accident_year.csv.bz2"
#'
#' @examples \dontrun{
#' make_filename(2013)
#' make_filename(2015)}
#'
#' @export
make_filename <- function(year) {
        year <- as.integer(year)
        sprintf("accident_%d.csv.bz2", year)
}

#' Make multiple data frames with MONTH and year columns
#'
#' With valid years and from files in the working directory, this
#' function makes a list with one or multiple data frames only with
#' MONTH and year colums. For invalid years returns NULL instead of a
#' data frame.
#'
#' @param years a numeric vector representing years
#'
#' @return a list with one or multiple objects with the classes "tbl_df",
#' "tbl" and "data.frame". For invalid years it returns NULL instead of a
#' data frame and a waring message specifying the invalid year(s). Each
#' data frame will have a lenght equal to its file in the working
#' directory
#'
#' @details this function needs make_filename() and fars_read() from
#' this package.
#'
#' @import dplyr
#'
#' @examples \dontrun{
#' years <- c(2013, 2014, 2015)
#' data <- fars_read_years(years)
#' data}
#'
#' @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)
                })
        })
}

#' Counts data number by month and year
#'
#' For valid years and from files in the working directory, this function
#' makes a data frame with MONTH and years columns (one column per year).
#' For each MONTH and year rows, it summarizes the data number.
#'
#' @param years a numeric vector
#'
#' @return an object of the class "tbl_df", "tbl" and "data.frame". For
#'  invalid years it returns a warning message.
#'
#' @details this function needs fars_read_years() from this package.
#'  As this function is necessary its details and notes are applicable.
#'
#' @import dplyr
#' @importFrom tidyr spread
#'
#' @note Preferably, the dplyr package must be load.
#'
#' @examples \dontrun{
#' years <- c(2013, 2014, 2015)
#' sum_data <- fars_summarize_years(years)
#' sum_data}
#'
#' @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)
}

#' Make a plot of the accidents in a state for a given year
#'
#' From a file in the working directory, this function makes a
#' representation of a state with dots representing accidents.
#' All previous for a valid state number and a valid year.
#'
#' @param state.num a number that represent a state
#' @param year a number representing a year
#'
#' @return a plot representing the given state with dots that represent
#' accidents. For an invalid state number it stops the execution and
#' returns an error message.If the given state doesn't have any accident
#' it returns a message.
#'
#' @details this function neeeds make_filename() and fars_read() from this
#' package. As this function is necessary, all its details and notes
#' must be consider.
#'
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @examples \dontrun{
#' fars_map_state(4, 2013)
#' fars_map_state(6, 2014)
#' fars_map_state(10, 2015)}
#'
#' @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)
        })
}
Juanin2691/fars documentation built on May 9, 2019, 3:26 a.m.