Skip to content

Commit

Permalink
Merge pull request #118 from tidy-finance/117-ensure-that-download_da…
Browse files Browse the repository at this point in the history
…ta-fails-gracefully-if-the-resource-is-not-available-or-has-changed

Add handle_download_error() function
  • Loading branch information
christophscheuch authored Nov 29, 2024
2 parents 495d542 + bfdcb09 commit b5c0988
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidyfinance
Title: Tidy Finance Helper Functions
Version: 0.4.1.9003
Version: 0.4.1.9004
Authors@R: c(
person("Christoph", "Scheuch", , "christoph.scheuch@gmail.com", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0009-0004-0423-6819")),
Expand Down
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# tidyfinance (development version)

## Bug fixes

* `download_macro_predictors()`, `download_factors()`, and `download_osap()` now fail gracefully with informative messages instead of errors or warnings.

## Improvements

* Updated `ccmxpf_linktable` to the new WRDS default `ccmxpf_lnkhist`.
Expand Down
25 changes: 23 additions & 2 deletions R/download_data_factors.R
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,16 @@ download_data_factors_ff <- function(
factors_ff_types <- list_supported_types_ff()
dataset <- factors_ff_types$dataset_name[factors_ff_types$type == type]

raw_data <- suppressMessages(frenchdata::download_french_data(dataset))
raw_data <- handle_download_error(
function() suppressMessages(frenchdata::download_french_data(dataset)),
fallback = tibble()
)

if (class(raw_data) != "french_dataset") {
cli::cli_inform("Returning an empty data set due to download failure.")
return(raw_data)
}

raw_data <- raw_data$subsets$data[[1]]

if (grepl("monthly", type, fixed = TRUE)) {
Expand Down Expand Up @@ -173,7 +182,19 @@ download_data_factors_q <- function(

factors_q_types <- list_supported_types_q()
dataset <- factors_q_types$dataset_name[factors_q_types$type == type]
raw_data <- suppressMessages(utils::read.csv(paste0(url, dataset)) |> as_tibble())

raw_data <- handle_download_error(
function(url) suppressWarnings(
suppressMessages(utils::read.csv(url)) |> as_tibble()
),
paste0(url, dataset),
fallback = tibble()
)

if (nrow(raw_data) == 0) {
cli::cli_inform("Returning an empty data set due to download failure.")
return(raw_data)
}

if (grepl("monthly", type, fixed = TRUE)) {
processed_data <- raw_data |>
Expand Down
30 changes: 27 additions & 3 deletions R/download_data_macro_predictors.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#'
#' @examples
#' \donttest{
#' macro_predictors_monthly <- download_data_macro_predictors("macro_predictors_monthly")
#' download_data_macro_predictors("macro_predictors_monthly")
#' }
#'
download_data_macro_predictors <- function(
Expand All @@ -49,12 +49,36 @@ download_data_macro_predictors <- function(
}

if (grepl("monthly", type, fixed = TRUE)) {
raw_data <- as_tibble(read.csv(build_macro_predictors_url("Monthly")))

raw_data <- handle_download_error(
function() suppressWarnings(
as_tibble(read.csv(build_macro_predictors_url("Monthly")))
),
fallback = tibble()
)

if (nrow(raw_data) == 0) {
cli::cli_inform("Returning an empty data set due to download failure.")
return(raw_data)
}

processed_data <- raw_data |>
mutate(date = ym(yyyymm))
}
if (grepl("quarterly", type, fixed = TRUE)) {
raw_data <- as_tibble(read.csv(build_macro_predictors_url("Quarterly")))

raw_data <- handle_download_error(
function() suppressWarnings(
as_tibble(read.csv(build_macro_predictors_url("Quarterly")))
),
fallback = tibble()
)

if (nrow(raw_data) == 0) {
cli::cli_inform("Returning an empty data set due to download failure.")
return(raw_data)
}

processed_data <- raw_data |>
mutate(
year = substr(yyyyq, 1, 4),
Expand Down
13 changes: 12 additions & 1 deletion R/download_data_osap.R
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,18 @@ download_data_osap <- function(
}

url <- paste0("https://drive.google.com/uc?export=download&id=", sheet_id)
raw_data <- as_tibble(read.csv(url))

raw_data <- handle_download_error(
function() suppressWarnings(
as_tibble(read.csv(url))
),
fallback = tibble()
)

if (nrow(raw_data) == 0) {
cli::cli_inform("Returning an empty data set due to download failure.")
return(raw_data)
}

processed_data <- raw_data |>
mutate(date = ymd(date))
Expand Down
17 changes: 17 additions & 0 deletions R/handle_download_error.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#' @keywords internal
#'
handle_download_error <- function(download_function, ..., fallback = NULL) {
tryCatch(
{
download_function(...)
},
error = function(e) {
cli::cli_inform(paste0(
"Failed to download or process the dataset. ",
"The resource may not be available, or the URL may have changed. ",
"Error message: {.message {e$message}}"
))
fallback
}
)
}
2 changes: 1 addition & 1 deletion man/download_data_macro_predictors.Rd

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

0 comments on commit b5c0988

Please sign in to comment.