#' @title Read data from FARS
#'
#' @description This is a function that reads the annual FARS data file
#' for concrete year properly specified by \code{filename} argument.
#' Filename could be generated with \code{make_filename} function.
#'
#' @details The FARS data should be R in the working directory
#'
#'
#' @param filename character string
#'
#' @return This function returns a data frame.
#'
#' @section Depends on:
#' \enumerate{
#' \item \code{\link[readr]{read_csv}} in the \code{readr} package.
#' \item \code{\link[dplyr]{tbl_df}} in the \code{dplyr} package.
#' }
#'
#' @examples
#'\dontrun{
#' fars_read("accident_2014.csv.bz2")
#' fars_read(make_filename(2014))
#'}
#'
#' @export
#' @importFrom dplyr tbl_df
#' @importFrom readr read_csv
#'
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)
}
#' @title Make filename for FARS annual data
#'
#' @description This is a function that, generates specific filename
#' needed to access the file of anual FARS data specified by \code{year} argument.
#' Filemane generated by this function could be used by \code{fars_read} function.
#'
#' @details If used inside fars_read single number agrument recommended
#'
#' @param year numeric vector that specifies the year for which the filename will be generated
#'
#' @return This function returns a character string
#'
#' @examples
#' \dontrun{
#' make_filename(2014)
#' fars_read(make_filename(2014))
#'}
#'
#' @export make_filename
make_filename <- function(year) {
year <- as.integer(year)
sprintf("accident_%d.csv.bz2", year)
}
#' @title Read multiple years FARS data
#'
#' @description This is a function that reads FARS data for one or more years
#' specified by \code{years} argument and returnes a list of corresponding data frames
#' (one data frame by year)
#'
#' @details Each year in separate data frame in the same list
#'
#' @param years numeric vector that specifies the years to read the data
#'
#' @return This function returns a list of data frames
#'
#' @section Depends on:
#' \enumerate{
#' \item \code{\link[dplyr]{mutate}} and \code{\link[dplyr]{select}} in the \code{dplyr} package.
#' }
#'
#' @examples
#' \dontrun{
#' fars_read_years(c(2014, 2015))
#' fars_read_years(2013:2015)
#' fars_read_years(list(2013, 2014, 2015))
#' }
#'
#'
#' @export
#' @importFrom dplyr mutate
#' @importFrom dplyr %>%
#' @importFrom dplyr select
#'
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)
})
})
}
#' @title Summarize FARS data for multiple years
#'
#' @description This is a function that reads and then summarises FARS data for one or more years
#' specified by \code{years} argument and returnes summary data frame.
#'
#' @details Summary table will be received
#'
#'
#' @param years numeric vector that specifies the years to summarise the data
#'
#' @return This function returns a list of data frames
#'
#' @section Depends on:
#' \enumerate{
#' \item \code{\link[dplyr]{bind_rows}}, \code{\link[dplyr]{group_by}} and \code{\link[dplyr]{summarize}} in the
#' \code{dplyr} package.
#' \item \code{\link[tidyr]{spread}} in the \code{tidyr} package.
#' }
#'
#'
#' @examples
#' \dontrun{
#' fars_summarize_years(c(2013, 2014, 2015))
#' fars_summarize_years(c(2013:2015))
#' fars_summarize_years(list(2013, 2014, 2015))
#' }
#'
#' @export fars_summarize_years
#' @importFrom dplyr bind_rows
#' @importFrom dplyr group_by
#' @importFrom dplyr summarize
#' @importFrom tidyr spread
#'
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)
}
#'
#' @title Map FARS data by state
#'
#' @description This is a function that reads FARS data for specified \code{year}
#' and then plots it on a contour map of the state specified by \code{state.num}
#'
#' @details Map of the state with dots of casualities will be ploted
#'
#' @param year numeric number that specifies the year to read the data for
#'
#' @param state.num numeric number from 1 to 56 that specifies the state to plot on the map
#'
#' @return This function returns a plot
#'
#' @section Depends on:
#' \enumerate{
#' \item \code{\link[dplyr]{filter}} in the \code{dplyr} package.
#' \item \code{\link[maps]{map}} in the \code{maps} package.
#' \item \code{\link[graphics]{points}} in the \code{graphics} package.
#' }
#'
#'
#' @examples
#' \dontrun{
#' fars_map_state(1, 2014)
#' fars_map_state("3", "2014")
#' }
#'
#' @export
#'
#' @importFrom dplyr filter
#' @importFrom dplyr %in%
#' @importFrom maps map
#' @importFrom graphics points
#'
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.