#' Reads in data from a comma separated value (.csv) file
#'
#' If \code{filename} does not exist in the specified path execution is halted.
#' It requires the \code{read_csv} function from readr and \code{tbl_df} from dplyr
#'
#' @param filename A .csv file name in the current directory or a path to a .csv file
#' @return A tbl_df of the data contained in \code{filename}
#'
#' @examples
#' fars_read(filename)
#' fars_read('foo.bar')
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)
}
#' Generates a formatted filename string
#'
#' It should be possible to cast \code{year} as an integer. It requires magrittr library
#'
#' @param year A number specifying the year of interest
#' @return A string specifying the full name of the file containing data for \code{year}
#'
#' @examples
#' make_filename(1979)
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' Read data from a list of specific years
#'
#' The data files must be located in current directory. It requires the dplyr package
#'
#' @param years A list of years which are to be read
#' @return A list of tibbles with columns MONTH and year
#' @examples
#' acc_years <- fars_read_years(c(2014,2015))
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)
})
})
}
#' Summarize number of accidents by year and month
#'
#' It requires the \code{group_by} and \code{summarize} from dplyr, \code{spread} from
#' the tidyr package and magrittr
#'
#' @param years A list of years which are to be read
#' @return A tbl_df with number of accidents by month and year
#'
#' @examples
#' fars_summarize_years(c(2014,2015))
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)
}
#' Plots accidents for given state and year if there are any
#'
#' It checks if \code{sate.num} is a valid identifier
#' It requires \code{filter} from dplyr, \code{map} from maps and \code{points} from graphics
#'
#' @param state.num An integer identifying the state to map
#' @param year An integer identifying the year from which to extract the accidents
#' @return A points plot of the accidents location within the state
#'
#' @examples
#' fars_map_state(1,2014)
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)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.