Skip to content

Commit ed2cd94

Browse files
committed
graceful_download
1 parent 99b7475 commit ed2cd94

File tree

2 files changed

+59
-15
lines changed

2 files changed

+59
-15
lines changed

NEWS.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pmlbr 0.2.2
2+
* File download fail gracefully if no internet connection is available
23

34
# pmlbr 0.2.1
45

R/pmlb.R

+58-15
Original file line numberDiff line numberDiff line change
@@ -32,24 +32,25 @@ fetch_data <- function(dataset_name, return_X_y = FALSE, local_cache_dir = NA, d
3232
stop("'return_X_y' must be TRUE or FALSE:\n * return_X_y is ", return_X_y, ".", call. = FALSE)
3333
}
3434

35-
3635
dataset_url <- paste0(
3736
GITHUB_URL, "/",
3837
dataset_name, "/",
3938
dataset_name,
4039
SUFFIX
4140
)
4241

43-
44-
4542
if (is.na(local_cache_dir)) {
4643
tmp <- tempfile()
47-
utils::download.file(dataset_url, tmp)
48-
dataset <- utils::read.csv(gzfile(tmp),
49-
sep = "\t",
50-
header = TRUE,
51-
stringsAsFactors = FALSE
52-
)
44+
if (!graceful_download(dataset_url, tmp)) {
45+
message("Continuing gracefully without the dataset.")
46+
} else {
47+
dataset <- utils::read.csv(
48+
gzfile(tmp),
49+
sep = "\t",
50+
header = TRUE,
51+
stringsAsFactors = FALSE
52+
)
53+
}
5354
} else {
5455
if (!file.exists(local_cache_dir)) {
5556
dir.create(file.path(local_cache_dir))
@@ -66,12 +67,15 @@ fetch_data <- function(dataset_name, return_X_y = FALSE, local_cache_dir = NA, d
6667
)
6768
# download file to cache and read it
6869
} else {
69-
utils::download.file(dataset_url, dataset_path)
70-
dataset <- utils::read.csv(dataset_path,
71-
sep = "\t",
72-
header = TRUE,
73-
stringsAsFactors = FALSE
74-
)
70+
if (!graceful_download(dataset_url, dataset_path)) {
71+
message("Continuing gracefully without the dataset.")
72+
} else {
73+
dataset <- utils::read.csv(dataset_path,
74+
sep = "\t",
75+
header = TRUE,
76+
stringsAsFactors = FALSE
77+
)
78+
}
7579
}
7680
}
7781

@@ -120,3 +124,42 @@ fetch_data <- function(dataset_name, return_X_y = FALSE, local_cache_dir = NA, d
120124
#' @docType package
121125
#' @name pmlb
122126
NULL
127+
128+
#' Download a File Gracefully with Retry Mechanism
129+
#'
130+
#' Attempts to download a file from a specified URL, retrying a set number of times if the download fails.
131+
#' This function meets CRAN's requirement for gracefully handling the use of internet resources by
132+
#' catching errors and returning a warning message if the download ultimately fails.
133+
#'
134+
#' @param url Character. The URL of the file to download.
135+
#' @param destfile Character. The path to the destination file where the downloaded content will be saved.
136+
#' @param retries Integer. The maximum number of download attempts (default is 3).
137+
#'
138+
#' @return Logical. Returns `TRUE` if the download succeeds, `FALSE` otherwise.
139+
#' @examples
140+
#' \dontrun{
141+
#' dataset_url <- "https://example.com/dataset.csv"
142+
#' tmp <- tempfile(fileext = ".csv")
143+
#' success <- download_file_gracefully(dataset_url, tmp)
144+
#' if (!success) {
145+
#' message("Continuing gracefully without the dataset.")
146+
#' }
147+
#' }
148+
graceful_download <- function(url, destfile, retries = 3) {
149+
attempt <- 1
150+
while (attempt <= retries) {
151+
tryCatch(
152+
{
153+
utils::download.file(url, destfile)
154+
message("Download successful.")
155+
return(TRUE)
156+
},
157+
error = function(e) {
158+
message(paste("Attempt", attempt, "failed:", e$message))
159+
attempt <<- attempt + 1
160+
}
161+
)
162+
}
163+
warning("Download failed after ", retries, " attempts.")
164+
return(FALSE)
165+
}

0 commit comments

Comments
 (0)