Skip to content

Commit

Permalink
Merge pull request #243 from ropensci/242-better-type-guessing
Browse files Browse the repository at this point in the history
Update type check logic, close #242
  • Loading branch information
Robinlovelace authored Jul 19, 2024
2 parents 6b74be0 + e6dc468 commit 3242e36
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 9 additions & 6 deletions R/get.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
#' @examples
#' \donttest{
#' if(curl::has_internet()) {
#' x = get_stats19(2022, silent = TRUE, format = TRUE)
#' class(x)
#' col = get_stats19(year = 2022, type = "collision")
#' cas2 = get_stats19(year = 2022, type = "casualty")
#' veh = get_stats19(year = 2022, type = "vehicle")
#' class(col)
#' # data.frame output
#' x = get_stats19(2022, silent = TRUE, output_format = "data.frame")
#' class(x)
Expand Down Expand Up @@ -97,8 +99,9 @@ get_stats19 = function(year = NULL,
silent = FALSE,
output_format = "tibble",
...) {
if(!exists("type")) {
stop("Type is required", call. = FALSE)
# Set type to "collision" if it's "accident" or similar:
if (grepl("acc", x = type, ignore.case = TRUE)) {
type = "collision"
}
if (!output_format %in% c("tibble", "data.frame", "sf", "ppp")) {
warning(
Expand Down Expand Up @@ -130,12 +133,12 @@ get_stats19 = function(year = NULL,
silent = silent)
read_in = NULL
# read in
if(grepl(type, "vehicles", ignore.case = TRUE)){
if(grepl("veh", x = type, ignore.case = TRUE)){
read_in = read_vehicles(
year = year,
data_dir = data_dir,
format = format)
} else if(grepl(type, "casualty", ignore.case = TRUE)) {
} else if(grepl("cas", x = type, ignore.case = TRUE)) {
read_in = read_casualties(
year = year,
data_dir = data_dir,
Expand Down
6 changes: 5 additions & 1 deletion tests/testthat/test-dl_stats19.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ test_that("dl_stats19 works for no data_dir", {
skip_download()
# remove tempdir
unlink(tempdir(), recursive = TRUE)
expect_message(dl_stats19(year = "2022", type = "collision"))
y = "2022"
t = "collision"
expect_message(dl_stats19(year = y, type = t))
# tempdir created.
file = find_file_name(years = y, type = t)
expect_match(file, t)
})

0 comments on commit 3242e36

Please sign in to comment.