-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_data.R
521 lines (457 loc) · 14.9 KB
/
get_data.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
################################################################################
#
#'
#' Retrieve datasets from specified DoH Data Drop folders
#'
#' A wrapper to `googledrive` functions to retrieve datasets from the **DoH Data
#' Drop** folders.
#'
#' @param tbl A tibble output produced by [datadrop_ls()] that lists the files
#' within a particular **DoH Data Drop** *Google Drive* folder
#' @param fn A character string composed of a word or words that can be used to
#' match to the name of a file within a particular **DoH Data Drop**
#' *Google Drive* folder listed in `tbl`.
#' @param path A character value for path for output file. If NULL, the
#' default file name used in *Google Drive* is used and the default location
#' is the working directory.
#' @param keep Logical. Should file be saved locally? Default to FALSE. If TRUE,
#' file is kept in the location specified in `path`. If `path` is NULL, the
#' file is kept in the working directory using the default file name used in
#' *Google Drive*.
#' @param overwrite Logical. If `path` already exists, should it be overwritten?
#' Default to FALSE.
#' @param verbose Logical. Should operation progress messages be shown? Default
#' to TRUE.
#'
#' @return A tibble of any of the following datasets:
#' 1. *Metadata - Sheets*;
#' 2. *Metadata - Fields*;
#' 3. *Case Information*;
#' 4. *DOH Data Collect - Daily Report*;
#' 5. *DOH Data Collect - Weekly Report*;
#' 6. *Testing Aggregates*;
#' 7. *Quarantine Facility Data - Daily Report*;
#' 8. *Quarantine Facility Data - Weekly Report*;
#' 9. *DOH Data Collect v3 - Baseline*;
#' 10. *DOH Data Collect v4 - Baseline*; and,
#' 11. *DDC TTMF Tracker v1*.
#' For Changelog, a named list of two tibbles - *List of Changes* and
#' *Most Common Changes*. If `keep` is TRUE, a copy of the specified
#' **DoH Data Drop** *Google Drive* file is saved in the location specified by
#' `path`.
#'
#' @examples
#' \dontrun{
#' library(googledrive)
#'
#' ## Authentication
#' googledrive::drive_auth_configure(api_key = Sys.getenv("GOOGLEDRIVE_TOKEN"))
#'
#' ## Deauthorise
#' googledrive::drive_deauth()
#'
#' ## Get tbl for files in latest Data Drop
#' library(magrittr)
#' x <- datadrop_id() %>% datadrop_ls()
#'
#' ## Retrieve case information data
#' datadrop_get(tbl = x, fn = "Case Information", path = tempfile())
#'
#' ## Retrieve latest changelog information
#' datadrop_get_changelog(tbl = x, path = tempfile())
#'
#' ## Retrieve latest metadata - sheets information
#' datadrop_get_sheets(tbl = x, path = tempfile())
#'
#' ## Retrieve latest metadata - fields information
#' datadrop_get_fields(tbl = x, path = tempfile())
#'
#' ## Retrieve latest cases information (same results as first example)
#' datadrop_get_cases(tbl = x, path = tempfile())
#'
#' ## Retrieve latest daily hospital beds and mechanical ventilators information
#' datadrop_get_cdaily(tbl = x, path = tempfile())
#'
#' ## Retrieve latest weekly PPE and other related equipment information
#' datadrop_get_cweekly(tbl = x, path = tempfile())
#'
#' ## Retrieve latest testing aggregates information
#' datadrop_get_tests(tbl = x, path = tempfile())
#'
#' ## Retrieve latest daily quarantine facility beds and mechanical ventilators
#' datadrop_get_qdaily(tbl = x, path = tempfile())
#'
#' ## Retrieve latest weekly quarantine facility PPE and other related equipment
#' datadrop_get_qweekly(tbl = x, path = tempfile())
#' }
#'
#' #datadrop_get_collectV3(tbl = x, path = tempfile())
#' #datadrop_get_collectV4(tbl = x, path = tempfile())
#' #datadrop_get_tracker(tbl = x, path = tempfile())
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get <- function(tbl, fn, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Deauthorise to access public Google Drive
#googledrive::drive_deauth()
## Get Google Drive file ID for specified tbl and fn
id <- datadrop_id_file(tbl = tbl, fn = fn)
ext <- get_ext(tbl = tbl, fn = fn)
if(!is.null(id)) {
## Download Google Drive file
datadrop_download(id = id,
path = path,
overwrite = overwrite,
verbose = verbose)
## Check if path is NULL
if (is.null(path)) {
path <- get_path(tbl = tbl, id = id)
}
## Check file extension
if(ext == ".csv") {
x <- read.csv(file = path)
x <- tibble::tibble(x)
} else {
x <- lapply(X = readxl::excel_sheets(path = path),
FUN = readxl::read_xlsx,
path = path)
## Rename output
names(x) <- c("List of Changes", "Most Common Changes")
}
## Keep file?
if (!keep) {
unlink(path)
}
} else {
x <- NULL
}
## Return x
return(x)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_changelog <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Changelog.xlsx
y <- datadrop_id_file(tbl = tbl, fn = "Changelog.xlsx")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No changelog information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.",
sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set changelog to NULL
changelog <- NULL
} else {
## Retrieve data
changelog <- datadrop_get(tbl = tbl, fn = "Changelog.xlsx",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(changelog)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_sheets <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Sheets file
y <- datadrop_id_file(tbl = tbl, fn = "Metadata - Sheets.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No metadata sheets information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.",
sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set sheets to NULL
sheets <- NULL
} else {
## Retrieve data
sheets <- datadrop_get(tbl = tbl, fn = "Metadata - Sheets.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(sheets)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_fields <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Fields file
y <- datadrop_id_file(tbl = tbl, fn = "Metadata - Fields.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No metadata fields information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.",
sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set fields to NULL
fields <- NULL
} else {
## Retrieve data
fields <- datadrop_get(tbl = tbl, fn = "Metadata - Fields.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(fields)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_cases <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Case Information file
y <- datadrop_id_file(tbl = tbl, fn = "Case Information.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No cases information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.",
sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set cases to NULL
cases <- NULL
} else {
## Retrieve data
cases <- datadrop_get(tbl = tbl, fn = "Case Information.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(cases)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_tests <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Testing file
y <- datadrop_id_file(tbl = tbl, fn = "Testing Aggregates.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No testing aggregates information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.",
sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set tests to NULL
tests <- NULL
} else {
## Retrieve data
tests <- datadrop_get(tbl = tbl, fn = "Testing Aggregates.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(tests)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_cdaily <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Daily report file
y <- datadrop_id_file(tbl = tbl, fn = "Collect - Daily Report.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No daily hospital beds and mechanical ventilator information on ",
.date,
". Try a date earlier or later than date specified. Returning NULL.", sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set facilities to NULL
facilities <- NULL
} else {
## Retrieve data
facilities <- datadrop_get(tbl = tbl, fn = "Collect - Daily Report.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(facilities)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_cweekly <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Weekly report file
y <- datadrop_id_file(tbl = tbl, fn = "Collect - Weekly Report.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No weekly PPE and medical personnel information on ", .date,
". Try a date earlier or later than date specified. Returning NULL.", sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set equipment to NULL
equipment <- NULL
} else {
## Retrieve data
equipment <- datadrop_get(tbl = tbl, fn = "Collect - Weekly Report.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(equipment)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_qdaily <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get Google Drive ID for Daily report file
y <- datadrop_id_file(tbl = tbl, fn = "Quarantine Facility Data - Daily Report.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No daily quarantine beds and mechanical ventilator information on ",
.date,
". Try a date earlier or later than date specified. Returning NULL.", sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set facilities to NULL
facilities <- NULL
} else {
## Retrieve data
facilities <- datadrop_get(tbl = tbl, fn = "Quarantine Facility Data - Daily Report.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(facilities)
}
################################################################################
#
#'
#' @rdname datadrop_get
#' @export
#'
#
################################################################################
datadrop_get_qweekly <- function(tbl, path = NULL, keep = FALSE,
overwrite = FALSE, verbose = TRUE) {
## Get list of contents of specified Google drive directory
y <- datadrop_id_file(tbl = tbl, fn = "Quarantine Facility Data - Weekly Report.csv")
.date <- get_drop_date(tbl = tbl)
if(is.null(y)) {
warning(
paste(
strwrap(
x = paste("No weekly quarantine PPE and medical personnel information on ",
.date,
". Try a date earlier or later than date specified. Returning NULL.", sep = ""),
width = 80
),
collapse = "\n"
)
)
## Set equipment to NULL
equipment <- NULL
} else {
## Retrieve data
equipment <- datadrop_get(tbl = tbl, fn = "Quarantine Facility Data - Weekly Report.csv",
path = path, keep = keep,
overwrite = overwrite, verbose = verbose)
}
## Return dataset
return(equipment)
}