Skip to content

Commit

Permalink
✨ Update downloading function for multiple years
Browse files Browse the repository at this point in the history
  • Loading branch information
NONONOexe committed Jan 15, 2025
1 parent 2f0a3d0 commit a381b29
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export(post_process_sub)
export(read_accident_data)
export(reset_config)
export(select_post_processor)
importFrom(cli,cli_abort)
importFrom(cli,cli_alert_success)
importFrom(curl,multi_download)
importFrom(lubridate,make_datetime)
importFrom(readr,locale)
importFrom(readr,read_csv)
Expand Down
26 changes: 13 additions & 13 deletions R/download-accident-data.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
#' @param type Type of data to download: one of "main", "sub", or "highway".
#' @param download_dir A directory where downloaded file is to saved
#' (default: `getwd()`).
#' @param year Year the traffic accident data was recorded.
#' Available from 2019 to 2023 (default: `2023`).
#' @param years Years the traffic accident data were recorded.
#' Available from 2019 to 2023. You can specify a single year or
#' vector of years(default: `2023`).
#' @return Path of the downloaded file (invisibly).
#' @export
#' @examples
Expand All @@ -23,33 +24,32 @@
#' download_accident_data("sub", "download-dir-path", 2023)
#' download_accident_data("highway", "download-dir-path", 2023)
#' }
download_accident_data <- function(type, download_dir = getwd(), year = 2023) {
download_accident_data <- function(type, download_dir = getwd(), years = 2023) {

# Mapping of type to actual file name part
type_map <- c(main = "honhyo", sub = "hojuhyo", highway = "kosokuhyo")

# Validation
valid_types <- names(type_map)
if (!type %in% valid_types) {
stop("Invalid type. Please specify one of: ",
paste(valid_types, collapse = ', '))
cli_abort("Invalid type. Please specify one of: {valid_types}.")
}

if (year < 2019 || 2023 < year) {
stop("The year must be between 2019 and 2023.")
if (any(years < 2019 | years > 2023)) {
invalid_years <- years[years < 2019 | years > 2023]
cli_abort("The `years` must be between 2019 and 2023. Invalid year(s) provided: {invalid_years}.")
}

# Use string manipulation to create the URL
page <- "https://www.npa.go.jp/publications/statistics/koutsuu/opendata"
url <- paste0(page, "/", year, "/", type_map[[type]], "_", year, ".csv")
urls <- paste0(page, "/", years, "/", type_map[[type]], "_", years, ".csv")

# Create destination file path
destfile <- file.path(download_dir, basename(url))
destfiles <- file.path(download_dir, basename(urls))

# Download
message("Downloading the file to: ", destfile)
downloaded_file_path <- download.file(url, destfile, quiet = TRUE)
message("Downloaded ", destfile, " successfly!")
downloaded_file_path <- multi_download(urls, destfiles)
cli_alert_success("Successfully downloaded {length(destfiles)} files to {download_dir}")

return(invisible(destfile))
return(invisible(destfiles))
}
3 changes: 3 additions & 0 deletions R/jpaccidents-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom cli cli_abort
#' @importFrom cli cli_alert_success
#' @importFrom curl multi_download
#' @importFrom lubridate make_datetime
#' @importFrom readr locale
#' @importFrom readr read_csv
Expand Down
7 changes: 4 additions & 3 deletions man/download_accident_data.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions tests/testthat/test-download-accident-data.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
test_that("`download_accident_data` works with valid inputs", {
local_mocked_bindings(download.file = function(url, destfile, quiet) {
file.copy(mock_accident_data_path, destfile, overwrite = TRUE)
local_mocked_bindings(multi_download = function(urls, destfiles) {
file.copy(mock_accident_data_path, destfiles, overwrite = TRUE)

return(destfile)
return(destfiles)
})

# Test with "main" data type
Expand Down Expand Up @@ -30,13 +30,19 @@ test_that("`download_accident_data` works with valid inputs", {
})

test_that("`download_accident_data` handles invalid data types", {
expect_error(download_accident_data("invalid", temp_dir, 2022),
"Invalid type. Please specify one of: main, sub, highway")
expect_error(
download_accident_data("invalid", temp_dir, 2022),
"Invalid type. Please specify one of: main, sub, and highway."
)
})

test_that("`download_accident_data` handles invalid years", {
expect_error(download_accident_data("main", temp_dir, 2018),
"The year must be between 2019 and 2023.")
expect_error(download_accident_data("main", temp_dir, 2024),
"The year must be between 2019 and 2023.")
expect_error(
download_accident_data("main", temp_dir, 2018),
"The `years` must be between 2019 and 2023. Invalid year\\(s\\) provided: 2018."
)
expect_error(
download_accident_data("main", temp_dir, 2024),
"The `years` must be between 2019 and 2023. Invalid year\\(s\\) provided: 2024."
)
})

0 comments on commit a381b29

Please sign in to comment.