R/fars_read_years.R

Defines functions fars_read_years

Documented in fars_read_years

#' fars_read_years
#'
#' Returns a list of tibbles indicating years and months of available data.
#' If the year is not available, a warning message will display. See 'Warning'.
#'
#'
#' @param years A numerical or character vector of four digit years in 'YYYY' format.
#'
#' @importFrom readr read_csv
#' @importFrom dplyr mutate select
#' @importFrom magrittr %>%
#'
#' @section Warning: invalid year: 'year'
#'
#' @return A list of objects of class‘tbl_df’, ‘tbl’ and 'data.frame' 
#'
#' @examples fars_read_years(2013:2015)
#'
#' @export
fars_read_years <- function(years) {
        lapply(years, function(year) {
                file <- make_filename(year)
                tryCatch({
                        dat <- fars_read(file) %>%
                        dplyr::mutate(year = year) %>%
                                dplyr::select_(quote(MONTH), quote(year))
                }, error = function(e) {
                        warning("invalid year: ", year)
                        return(NULL)
                })
        })
}
JustinMShea/Fatality-Analysis-Reporting-System documentation built on Sept. 20, 2017, 12:01 a.m.