@@ -34,13 +34,14 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
34
34
if (! dir.exists(dl_hydat_here )) {
35
35
dir.create(dl_hydat_here )
36
36
message(crayon :: blue(" You have downloaded hydat to" , dl_hydat_here ))
37
- message(crayon :: blue(" See ?hy_set_default_db to change where tidyhydat looks for HYDAT" ))
37
+ message(crayon :: blue(
38
+ " See ?hy_set_default_db to change where tidyhydat looks for HYDAT"
39
+ ))
38
40
}
39
41
}
40
42
41
43
if (! is.logical(ask )) stop(" Parameter ask must be a logical" )
42
44
43
-
44
45
# # Create actual hydat_path
45
46
hydat_path <- file.path(dl_hydat_here , " Hydat.sqlite3" )
46
47
@@ -52,7 +53,6 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
52
53
existing_hydat <- " HYDAT not present"
53
54
}
54
55
55
-
56
56
new_hydat <- hy_remote()
57
57
# Make the download URL
58
58
url <- paste0(hy_base_url(), " Hydat_sqlite3_" , new_hydat , " .zip" )
@@ -62,30 +62,40 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
62
62
req <- tidyhydat_perform(req )
63
63
httr2 :: resp_check_status(req )
64
64
65
- size <- round(as.numeric(
66
- httr2 :: resp_header(req , " Content-Length" )
67
- ) / 1000000 , 0 )
68
-
65
+ size <- round(
66
+ as.numeric(
67
+ httr2 :: resp_header(req , " Content-Length" )
68
+ ) /
69
+ 1000000 ,
70
+ 0
71
+ )
69
72
70
73
# # Do we need to download a new version?
71
- if (new_hydat == existing_hydat & ask ) { # DB exists and no new version
74
+ if (new_hydat == existing_hydat & ask ) {
75
+ # DB exists and no new version
72
76
msg <- paste0(
73
77
" The existing local version of HYDAT, published on " ,
74
78
lubridate :: ymd(existing_hydat ),
75
79
" , is the most recent version available. \n Do you wish to overwrite it? \n Downloading HYDAT could take up to 10 minutes (" ,
76
- size , " MB)."
80
+ size ,
81
+ " MB)."
77
82
)
78
83
dl_overwrite <- ask(msg )
79
84
} else {
80
85
dl_overwrite <- TRUE
81
86
}
82
87
83
88
if (! dl_overwrite ) {
84
- info(" HYDAT is updated on a quarterly basis, check again soon for an updated version." )
89
+ info(
90
+ " HYDAT is updated on a quarterly basis, check again soon for an updated version."
91
+ )
85
92
}
86
- if (new_hydat != existing_hydat & ask ) { # New DB available or no local DB at all
93
+ if (new_hydat != existing_hydat & ask ) {
94
+ # New DB available or no local DB at all
87
95
msg <- paste0(
88
- " This version of HYDAT is " , size , " MB in size and will take some time to download.
96
+ " This version of HYDAT is " ,
97
+ size ,
98
+ " MB in size and will take some time to download.
89
99
\n This will remove any older versions of HYDAT, if applicable. \n Is that okay?"
90
100
)
91
101
ans <- ask(msg )
@@ -99,12 +109,18 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
99
109
green_message(paste0(" Downloading HYDAT to " , dl_hydat_here ))
100
110
}
101
111
102
-
103
112
if (dl_overwrite ) {
104
113
if (new_hydat == existing_hydat ) {
105
- info(paste0(" Your local copy of HYDAT published on " , crayon :: blue(lubridate :: ymd(new_hydat )), " will be overwritten." ))
114
+ info(paste0(
115
+ " Your local copy of HYDAT published on " ,
116
+ crayon :: blue(lubridate :: ymd(new_hydat )),
117
+ " will be overwritten."
118
+ ))
106
119
} else {
107
- info(paste0(" Downloading new version of HYDAT created on " , crayon :: blue(lubridate :: ymd(new_hydat ))))
120
+ info(paste0(
121
+ " Downloading new version of HYDAT created on " ,
122
+ crayon :: blue(lubridate :: ymd(new_hydat ))
123
+ ))
108
124
}
109
125
110
126
# # temporary path to save
@@ -130,7 +146,6 @@ download_hydat <- function(dl_hydat_here = NULL, ask = TRUE) {
130
146
overwrite = TRUE
131
147
)
132
148
133
-
134
149
if (file.exists(hydat_path )) {
135
150
congrats(" HYDAT successfully downloaded" )
136
151
} else {
@@ -160,10 +175,10 @@ hy_remote <- function() {
160
175
req <- tidyhydat_perform(req )
161
176
resp <- httr2 :: resp_check_status(req )
162
177
163
-
164
178
raw_date <- substr(
165
179
gsub(" ^.*\\ Hydat_sqlite3_" , " " , httr2 :: resp_body_string(req )),
166
- 1 , 8
180
+ 1 ,
181
+ 8
167
182
)
168
183
169
184
raw_date
@@ -182,7 +197,6 @@ hy_check <- function(hydat_path = NULL) {
182
197
red_message(paste0(tbl_diff , " \n " ))
183
198
}
184
199
185
-
186
200
invisible (lapply(have_tbls , function (x ) {
187
201
tbl_rows <- dplyr :: tbl(con , x ) | >
188
202
utils :: head(1 ) | >
0 commit comments