This function checks whether a given file exists or not. If the file exists , it reads
the file into a data frame. Otherwise it throws an error with the message "File
library(dplyr) list.files() file.exists("accident_2013.csv")
fars_read <- function(filename) { tryCatch({ data1 <- suppressMessages({ lapply(system.file('extdata', filename, package = 'mynewpackage'), read.csv) }) }, error = function(e) { stop("file '", filename, "' does not exist") }) data2<-as.data.frame(data1) dplyr::tbl_df(data2) } #head(data1) fars_read("accident_2013.csv")
This function returns the name of the file by formatting it with user inputted year.This function will also print the name of the file by concatenating string 'accident' with the year provided by the user.
make_filename <- function(year) { year <- as.integer(year) sprintf("accident_%d.csv", year) } make_filename(2013)
This function calls the make_filename function to return the name of the input file. It then reads the file and select and return the year and month present in the file for that year. If the year provided by user is not a valid year. it will throw an error. Package imported by this function is dplyr for mutate and select function.
fars_read_years <- function(years) { file<-dat<-MONTH<-NULL lapply(years, function(year) { file <- make_filename(year) tryCatch({ dat <- fars_read(file) dat%>%dplyr::mutate_( year = ~year) %>% dplyr::select_(~MONTH, ~year) }, error = function(e) { warning("invalid year: ", year) return(NULL) }) }) } fars_read_years(2013)
This function reads the input file for the year provided by the user and then returns the number of observation for each month of the given year.If the year provided by user is not a valid year,this function will throw an error.Package imported by this function is dplyr for bind_rows, group_by and summarize function.
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_(key_col='year',value_col='n') } fars_summarize_years(2013)
This function reads the input file for the year provided by the user and returns a geographical map depicting the fatal injuries suffered in motor vehicle traffic crashes.If the year provided by user is not a valid year,this function will throw an error.Package imported by this function is dplyr for filter function,maps for map function and graphics for points function.
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) }) } fars_map_state(1,2013)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.