87
87
88
88
# ' Creates an `epi_df` object
89
89
# '
90
- # ' Creates a new `epi_df` object. By default, builds an empty tibble with the
90
+ # ' Creates a new `epi_df` object. By default, builds an empty tibble with the
91
91
# ' correct metadata for an `epi_df` object (ie. `geo_type`, `time_type`, and `as_of`).
92
92
# ' Refer to the below info. about the arguments for more details.
93
93
# '
@@ -107,18 +107,18 @@ NULL
107
107
# ' `epi_df` object. The metadata will have `geo_type`, `time_type`, and
108
108
# ' `as_of` fields; named entries from the passed list will be included as
109
109
# ' well. If your tibble has additional keys, be sure to specify them as a
110
- # ' character vector in the `other_keys` component of `additional_metadata`.
110
+ # ' character vector in the `other_keys` component of `additional_metadata`.
111
111
# ' @param ... Additional arguments passed to methods.
112
112
# ' @return An `epi_df` object.
113
- # '
113
+ # '
114
114
# ' @export
115
- new_epi_df = function (x = tibble :: tibble(), geo_type , time_type , as_of ,
116
- additional_metadata = list (), ... ) {
115
+ new_epi_df <- function (x = tibble :: tibble(), geo_type , time_type , as_of ,
116
+ additional_metadata = list (), ... ) {
117
117
# Check that we have a data frame
118
118
if (! is.data.frame(x )) {
119
119
Abort(" `x` must be a data frame." )
120
120
}
121
-
121
+
122
122
if (! is.list(additional_metadata )) {
123
123
Abort(" `additional_metadata` must be a list type." )
124
124
}
@@ -128,52 +128,55 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
128
128
129
129
# If geo type is missing, then try to guess it
130
130
if (missing(geo_type )) {
131
- geo_type = guess_geo_type(x $ geo_value )
131
+ geo_type <- guess_geo_type(x $ geo_value )
132
132
}
133
-
133
+
134
134
# If time type is missing, then try to guess it
135
135
if (missing(time_type )) {
136
- time_type = guess_time_type(x $ time_value )
136
+ time_type <- guess_time_type(x $ time_value )
137
137
}
138
-
138
+
139
139
# If as_of is missing, then try to guess it
140
140
if (missing(as_of )) {
141
141
# First check the metadata for an as_of field
142
142
if (" metadata" %in% names(attributes(x )) &&
143
- " as_of" %in% names(attributes(x )$ metadata )) {
144
- as_of = attributes(x )$ metadata $ as_of
143
+ " as_of" %in% names(attributes(x )$ metadata )) {
144
+ as_of <- attributes(x )$ metadata $ as_of
145
145
}
146
-
146
+
147
147
# Next check for as_of, issue, or version columns
148
- else if (" as_of" %in% names(x )) as_of = max(x $ as_of )
149
- else if (" issue" %in% names(x )) as_of = max(x $ issue )
150
- else if (" version" %in% names(x )) as_of = max(x $ version )
151
-
152
- # If we got here then we failed
153
- else as_of = Sys.time() # Use the current day-time
148
+ else if (" as_of" %in% names(x )) {
149
+ as_of <- max(x $ as_of )
150
+ } else if (" issue" %in% names(x )) {
151
+ as_of <- max(x $ issue )
152
+ } else if (" version" %in% names(x )) {
153
+ as_of <- max(x $ version )
154
+ } # If we got here then we failed
155
+ else {
156
+ as_of <- Sys.time()
157
+ } # Use the current day-time
154
158
}
155
-
159
+
156
160
# Define metadata fields
157
- metadata = list ()
158
- metadata $ geo_type = geo_type
159
- metadata $ time_type = time_type
160
- metadata $ as_of = as_of
161
- metadata = c(metadata , additional_metadata )
162
-
161
+ metadata <- list ()
162
+ metadata $ geo_type <- geo_type
163
+ metadata $ time_type <- time_type
164
+ metadata $ as_of <- as_of
165
+ metadata <- c(metadata , additional_metadata )
166
+
163
167
# Reorder columns (geo_value, time_value, ...)
164
- if (sum(dim(x )) != 0 ){
168
+ if (sum(dim(x )) != 0 ) {
165
169
cols_to_put_first <- c(" geo_value" , " time_value" )
166
170
x <- x [, c(
167
171
cols_to_put_first ,
168
172
# All other columns
169
173
names(x )[! (names(x ) %in% cols_to_put_first )]
170
- )
171
- ]
174
+ )]
172
175
}
173
-
176
+
174
177
# Apply epi_df class, attach metadata, and return
175
- class(x ) = c(" epi_df" , class(x ))
176
- attributes(x )$ metadata = metadata
178
+ class(x ) <- c(" epi_df" , class(x ))
179
+ attributes(x )$ metadata <- metadata
177
180
return (x )
178
181
}
179
182
@@ -205,77 +208,85 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
205
208
# ' @return An `epi_df` object.
206
209
# '
207
210
# ' @export
208
- # ' @examples
211
+ # ' @examples
209
212
# ' # Convert a `tsibble` that has county code as an extra key
210
213
# ' # Notice that county code should be a character string to preserve any leading zeroes
211
- # '
214
+ # '
212
215
# ' ex1_input <- tibble::tibble(
213
216
# ' geo_value = rep(c("ca", "fl", "pa"), each = 3),
214
- # ' county_code = c("06059","06061","06067",
215
- # ' "12111","12113","12117",
216
- # ' "42101", "42103","42105"),
217
+ # ' county_code = c(
218
+ # ' "06059", "06061", "06067",
219
+ # ' "12111", "12113", "12117",
220
+ # ' "42101", "42103", "42105"
221
+ # ' ),
217
222
# ' time_value = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
218
- # ' by = "day"), length.out = length(geo_value)),
223
+ # ' by = "day"
224
+ # ' ), length.out = length(geo_value)),
219
225
# ' value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))
220
- # ' ) %>%
226
+ # ' ) %>%
221
227
# ' tsibble::as_tsibble(index = time_value, key = c(geo_value, county_code))
222
- # '
228
+ # '
223
229
# ' # The `other_keys` metadata (`"county_code"` in this case) is automatically
224
230
# ' # inferred from the `tsibble`'s `key`:
225
231
# ' ex1 <- as_epi_df(x = ex1_input, geo_type = "state", time_type = "day", as_of = "2020-06-03")
226
- # ' attr(ex1,"metadata")[["other_keys"]]
227
- # '
228
- # '
229
- # '
232
+ # ' attr(ex1, "metadata")[["other_keys"]]
233
+ # '
234
+ # '
235
+ # '
230
236
# ' # Dealing with misspecified column names:
231
237
# ' # Geographical and temporal information must be provided in columns named
232
238
# ' # `geo_value` and `time_value`; if we start from a data frame with a
233
239
# ' # different format, it must be converted to use `geo_value` and `time_value`
234
240
# ' # before calling `as_epi_df`.
235
- # '
241
+ # '
236
242
# ' ex2_input <- tibble::tibble(
237
243
# ' state = rep(c("ca", "fl", "pa"), each = 3), # misnamed
238
244
# ' pol = rep(c("blue", "swing", "swing"), each = 3), # extra key
239
245
# ' reported_date = rep(seq(as.Date("2020-06-01"), as.Date("2020-06-03"),
240
- # ' by = "day"), length.out = length(state)), # misnamed
246
+ # ' by = "day"
247
+ # ' ), length.out = length(state)), # misnamed
241
248
# ' value = 1:length(state) + 0.01 * rnorm(length(state))
242
- # ' )
243
- # '
249
+ # ' )
250
+ # '
244
251
# ' print(ex2_input)
245
- # '
246
- # ' ex2 <- ex2_input %>% dplyr::rename(geo_value = state, time_value = reported_date) %>%
247
- # ' as_epi_df(geo_type = "state", as_of = "2020-06-03",
248
- # ' additional_metadata = list(other_keys = "pol"))
249
- # '
250
- # ' attr(ex2,"metadata")
251
- # '
252
- # '
253
- # '
252
+ # '
253
+ # ' ex2 <- ex2_input %>%
254
+ # ' dplyr::rename(geo_value = state, time_value = reported_date) %>%
255
+ # ' as_epi_df(
256
+ # ' geo_type = "state", as_of = "2020-06-03",
257
+ # ' additional_metadata = list(other_keys = "pol")
258
+ # ' )
259
+ # '
260
+ # ' attr(ex2, "metadata")
261
+ # '
262
+ # '
263
+ # '
254
264
# ' # Adding additional keys to an `epi_df` object
255
- # '
265
+ # '
256
266
# ' ex3_input <- jhu_csse_county_level_subset %>%
257
267
# ' dplyr::filter(time_value > "2021-12-01", state_name == "Massachusetts") %>%
258
- # ' dplyr::slice_tail(n = 6)
259
- # '
260
- # ' ex3 <- ex3_input %>%
268
+ # ' dplyr::slice_tail(n = 6)
269
+ # '
270
+ # ' ex3 <- ex3_input %>%
261
271
# ' tsibble::as_tsibble() %>% # needed to add the additional metadata
262
272
# ' # add 2 extra keys
263
273
# ' dplyr::mutate(
264
- # ' state = rep("MA",6),
265
- # ' pol = rep(c("blue", "swing", "swing"), each = 2)) %>%
266
- # ' # the 2 extra keys we added have to be specified in the other_keys
274
+ # ' state = rep("MA", 6),
275
+ # ' pol = rep(c("blue", "swing", "swing"), each = 2)
276
+ # ' ) %>%
277
+ # ' # the 2 extra keys we added have to be specified in the other_keys
267
278
# ' # component of additional_metadata.
268
279
# ' as_epi_df(additional_metadata = list(other_keys = c("state", "pol")))
269
- # '
270
- # ' attr(ex3,"metadata")
271
- as_epi_df = function (x , ... ) {
280
+ # '
281
+ # ' attr(ex3, "metadata")
282
+ as_epi_df <- function (x , ... ) {
272
283
UseMethod(" as_epi_df" )
273
284
}
274
285
275
286
# ' @method as_epi_df epi_df
276
287
# ' @describeIn as_epi_df Simply returns the `epi_df` object unchanged.
277
288
# ' @export
278
- as_epi_df.epi_df = function (x , ... ) {
289
+ as_epi_df.epi_df <- function (x , ... ) {
279
290
return (x )
280
291
}
281
292
@@ -289,27 +300,31 @@ as_epi_df.epi_df = function(x, ...) {
289
300
# ' be used.
290
301
# ' @importFrom rlang .data
291
302
# ' @export
292
- as_epi_df.tbl_df = function (x , geo_type , time_type , as_of ,
293
- additional_metadata = list (), ... ) {
303
+ as_epi_df.tbl_df <- function (x , geo_type , time_type , as_of ,
304
+ additional_metadata = list (), ... ) {
294
305
# Check that we have geo_value and time_value columns
295
306
if (! (" geo_value" %in% names(x ))) {
296
307
Abort(" `x` must contain a `geo_value` column." )
297
308
}
298
309
if (! (" time_value" %in% names(x ))) {
299
310
Abort(" `x` must contain a `time_value` column." )
300
311
}
301
-
302
- new_epi_df(x , geo_type , time_type , as_of ,
303
- additional_metadata , ... )
312
+
313
+ new_epi_df(
314
+ x , geo_type , time_type , as_of ,
315
+ additional_metadata , ...
316
+ )
304
317
}
305
318
306
319
# ' @method as_epi_df data.frame
307
320
# ' @describeIn as_epi_df Works analogously to `as_epi_df.tbl_df()`.
308
321
# ' @export
309
- as_epi_df.data.frame = function (x , geo_type , time_type , as_of ,
310
- additional_metadata = list (), ... ) {
311
- as_epi_df.tbl_df(tibble :: as_tibble(x ), geo_type , time_type , as_of ,
312
- additional_metadata , ... )
322
+ as_epi_df.data.frame <- function (x , geo_type , time_type , as_of ,
323
+ additional_metadata = list (), ... ) {
324
+ as_epi_df.tbl_df(
325
+ tibble :: as_tibble(x ), geo_type , time_type , as_of ,
326
+ additional_metadata , ...
327
+ )
313
328
}
314
329
315
330
# ' @method as_epi_df tbl_ts
@@ -318,23 +333,26 @@ as_epi_df.data.frame = function(x, geo_type, time_type, as_of,
318
333
# ' "geo_value") are added to the metadata of the returned object, under the
319
334
# ' `other_keys` field.
320
335
# ' @export
321
- as_epi_df.tbl_ts = function (x , geo_type , time_type , as_of ,
322
- additional_metadata = list (), ... ) {
323
- tsibble_other_keys = setdiff(tsibble :: key_vars(x ), " geo_value" )
336
+ as_epi_df.tbl_ts <- function (x , geo_type , time_type , as_of ,
337
+ additional_metadata = list (), ... ) {
338
+ tsibble_other_keys <- setdiff(tsibble :: key_vars(x ), " geo_value" )
324
339
if (length(tsibble_other_keys ) != 0 ) {
325
- additional_metadata $ other_keys = unique(
326
- c(additional_metadata $ other_keys , tsibble_other_keys ))
340
+ additional_metadata $ other_keys <- unique(
341
+ c(additional_metadata $ other_keys , tsibble_other_keys )
342
+ )
327
343
}
328
- as_epi_df.tbl_df(tibble :: as_tibble(x ), geo_type , time_type , as_of ,
329
- additional_metadata , ... )
344
+ as_epi_df.tbl_df(
345
+ tibble :: as_tibble(x ), geo_type , time_type , as_of ,
346
+ additional_metadata , ...
347
+ )
330
348
}
331
349
332
350
# ' Test for `epi_df` format
333
351
# '
334
352
# ' @param x An object.
335
353
# ' @return `TRUE` if the object inherits from `epi_df`.
336
- # '
354
+ # '
337
355
# ' @export
338
- is_epi_df = function (x ) {
356
+ is_epi_df <- function (x ) {
339
357
inherits(x , " epi_df" )
340
358
}
0 commit comments