@@ -32,24 +32,25 @@ fetch_data <- function(dataset_name, return_X_y = FALSE, local_cache_dir = NA, d
32
32
stop(" 'return_X_y' must be TRUE or FALSE:\n * return_X_y is " , return_X_y , " ." , call. = FALSE )
33
33
}
34
34
35
-
36
35
dataset_url <- paste0(
37
36
GITHUB_URL , " /" ,
38
37
dataset_name , " /" ,
39
38
dataset_name ,
40
39
SUFFIX
41
40
)
42
41
43
-
44
-
45
42
if (is.na(local_cache_dir )) {
46
43
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
+ }
53
54
} else {
54
55
if (! file.exists(local_cache_dir )) {
55
56
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
66
67
)
67
68
# download file to cache and read it
68
69
} 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
+ }
75
79
}
76
80
}
77
81
@@ -120,3 +124,42 @@ fetch_data <- function(dataset_name, return_X_y = FALSE, local_cache_dir = NA, d
120
124
# ' @docType package
121
125
# ' @name pmlb
122
126
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