Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in BITRE download; corrupted excel file? #51

Open
johann-wagner opened this issue Oct 29, 2024 · 0 comments
Open

Error in BITRE download; corrupted excel file? #51

johann-wagner opened this issue Oct 29, 2024 · 0 comments

Comments

@johann-wagner
Copy link
Contributor

I have been getting an error when downloading the "latest" data from BITRE website.

library(ozroaddeaths)
oz_road_fatalities(source = "latest")
#> Error in utils::unzip(zip_path, list = TRUE): error -103 with zipfile in unzGetCurrentFileInfo

It seems the underlying issue is in get_bitre_hard_coded() function, specifically download.file().

get_bitre_hard_coded <- function(group = "fatal_crashes") {
  rlang::arg_match(group, c("fatal_crashes", "fatalities"))
  
  # Date format: "sep2024"
  # If current date is 2024-10, this will produce
  # "oct2024", "sep2024", ..., "jan2024", "dec2023", ..., "jan2023"
  current_month_num <- as.integer(format(Sys.time(), "%m"))
  months <- tolower(month.abb[current_month_num:1])
  current_year <- as.integer(format(Sys.time(), "%Y"))
  past_year <- current_year - 1
  date_replacement <- c(paste0(months, current_year),
                        paste0(tolower(rev(month.abb)), past_year))
  
  # Insert dates into the link template.
  links <- glue::glue("https://www.bitre.gov.au/sites/default/files/documents/bitre_{group}_{date_replacement}.xlsx")
  
  # Try links one by one until one works.
  temp_xlsx <- tempfile()
  target_link <- NA
  for (link in links) {
    error <- try(suppressWarnings(utils::download.file(link, quiet = TRUE, destfile = temp_xlsx)),
                 silent = TRUE)
    if (!inherits(error, "try-error")) {
      target_link <- link
      break
    }
  }
  
  # If there is no valid link, return an empty string.
  if (is.na(target_link)) {
    return("")
  } else {
    return(temp_xlsx)
  }
}

get_bitre_hard_coded("fatal_crashes") |> 
  readxl::read_xlsx(sheet = 2, skip = 4)
#> Error in utils::unzip(zip_path, list = TRUE): error -103 with zipfile in unzGetCurrentFileInfo

This very likely could be a local issue (local network/wifi/port/etc.), so I'm curious whether others are getting this error?

Created on 2024-10-29 with reprex v2.1.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant