This set of functions compose a Dummy R package that helps me to learn how to build and document R packages. For more details on learning how to build R packages see the Coursera Building R Packages Course at http://coursera.org.
To test the far_funtions package functionality please follow the next directions
Instructions for testing the Package Functionality Accuracy
Use "test_dir("~/Desktop/CursoR2/farsfunctions/R/.")" to run all the test available in the fars_functions Package's R folder (test located inside the test_fars_functions.R script)
or
Use "test_file("~/Desktop/CursoR2/farsfunctions/R/test_farsfunctions.R")" to run all the test available inside the testing script test_fars_functions.R located inside the R folder of the fars_functions Package.
The code "fars_functions.R" must be sourced before executing the testing
or
Use Test your package with Ctrl/Cmd + Shift + T or devtools::test() that will execute the testthat.R file who looks inside the "tests dirrectory" at ("~farsfunctions/tests/testthat/.")" to run all the test available in the fars_functions Package's tests/thestthat folder (test located inside the test_fars_functions.R script)
The far_functions package contains a set of functions for manipulating the data from the US National Highway Traffic Safety Administration's Fatality Analysis Reporting System.
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) }
Some examples for using correctly and incorrectly the fars_read(filename) function are:
Correct way to use the fars_read(filename)
fars_read("/Users/1/Desktop/data/fars_data.zip")
Incorrect way to use the fars_read(filename)
fars_read()
make_filename <- function(year) { year <- as.integer(year) sprintf("accident_%d.csv.bz2", year) }
Some examples for using correctly and incorrectly the make_filename(year) function are:
Correct way to use the make_filename(year)
make_filename("2014")
Incorrect way to use the make_filename(year)
make_filename()
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) }) }) }
Some examples for using correctly and incorrectly the fars_read_years(years) function are:
Correct way to use the fars_read_years(years)
fars_read_years(c(2013,2014))
Incorrect way to use the fars_read_years(years)
fars_read_years() fars_read_years("2013","2014")
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) }
Some examples for using correctly and incorrectly the fars_summarize_years(years) function are:
Correct way to use the fars_summarize_years(years)
fars_read_years(c(2013,2014))
Incorrect way to use the fars_summarize_years(years)
fars_read_years() fars_read_years("2013","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) }) }
Some examples for using correctly and incorrectly the fars_summarize_years(years) function are:
Correct way to use the fars_map_state(state.num,year)
fars_map_state(10,2014)
Incorrect way to use the fars_map_state(state.num,year)
fars_map_state() fars_map_state("10,2014")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.