#' Read-in a file and return it as a tibble.
#'
#' This is a function for reading csv-files and returning the data as a tibble. If the file does not exist the function returns an error with
#' the non-existing file-name.
#' Files ending with .gz, .bz2, .xz, or .zip will be automatically uncompressed and files starting with ttp://, https://, ftp://, or ftps://
#' will be downloaded. If the the file to read is not a csv-file, the function produces an error for an invalid file argument.
#'
#' @param filename A path to a file as a single string or vector.
#'
#' @return This function returns the data as a tibble.
#'
#' @importFrom readr read_csv
#' @importFrom dplyr tbl_df
#'
#' @examples
#' fars_read(make_filename(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)
}
#' Returns a filename.
#'
#' This function takes \code{year} as an input variable and returns a filename containing the input \code{year}.
#' It will return "accident_NA.csv.bz2" if the input argument \code{year} is not specified correctly.
#' Additionally, a warning message will be printed.
#'
#' @param year A character or numeric input variable that can be converted into an integer.
#'
#' @return This function returns a filename with input \code{year} as a string.
#'
#' @examples
#' make_filename(2013)
#' make_filename(c("2013","2014"))
#'
#' @export
make_filename <- function(year) {
year <- as.integer(year)
filename <- sprintf("accident_%d.csv.bz2", year)
filename <- system.file('extdata', "accident_2013.csv.bz2", package = 'farsdata')
filename
}
#' Returns month and year from data
#'
#' This function takes \code{years} as an input and automatically reads the corresponding data. Subsequently the month
#' and the year for the specified \code{years} are returned as a list of tibbles.
#'
#' @param years A character- or numeric vector. It specifies the year for which one wants to read the data from.
#'
#' @return This function returns a list of tibbles containing the month- and the year columns for the specified data. In case the the file for a specific year does not exist,
#' the function will return \code{NULL} and print a warning message.
#'
#' @importFrom dplyr mutate select
#' @importFrom magrittr "%>%"
#' @examples
#' fars_read_years(2013)
#' fars_read_years(c("2014","2015"))
#'
#' @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)
})
})
}
#' Returns number of occurances for every month per year
#'
#' This function returns the occurances of each month per year from the data-set. The result is returned as a tibble.
#' It will return the correct tibble for valid input \code{years}. Otherwise it will return an error and/or a warning message.
#'
#' @param years A character or numeric vector. It specifies the year for which one wants to read the data from.
#'
#' @return This function returns a tibble, which includes the occurances for each month per year from the data-set.
#'
#' @importFrom dplyr bind_rows group_by summarize
#' @importFrom tidyr spread
#' @importFrom magrittr "%>%"
#'
#' @examples
#' fars_summarize_years(2013)
#' fars_summarize_years(c("2014","2015"))
#'
#' @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)
}
#' Plots a map from accident data
#'
#' This function takes \code{state.num} and \code{year} as input agruments and returns a map with the corresponding
#' data points. If the input variable \code{state.num} is not represented in the data-set the function will return
#' an error message.
#'
#' @param state.num A character or numeric vector. It specifies the \code{STATE} of the observations.
#' @param year A character or numeric vector to specify the \code{year} to read the data from.
#'
#' @return This function plots a map with points, representing the individual accident observations. If there
#' are no accidents for a given \code{STATE} the function returns \code{NULL}
#'
#' @importFrom dplyr filter
#' @importFrom maps map
#' @importFrom graphics points
#'
#' @examples
#' fars_map_state(4,2013)
#' fars_map_state(1,2014)
#'
#' @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)
})
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.