Skip to content

Commit 58ed6b4

Browse files
Update R/epi_df.R
Co-authored-by: brookslogan <[email protected]>
1 parent 71e11f7 commit 58ed6b4

File tree

1 file changed

+104
-86
lines changed

1 file changed

+104
-86
lines changed

R/epi_df.R

+104-86
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ NULL
8787

8888
#' Creates an `epi_df` object
8989
#'
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
9191
#' correct metadata for an `epi_df` object (ie. `geo_type`, `time_type`, and `as_of`).
9292
#' Refer to the below info. about the arguments for more details.
9393
#'
@@ -107,18 +107,18 @@ NULL
107107
#' `epi_df` object. The metadata will have `geo_type`, `time_type`, and
108108
#' `as_of` fields; named entries from the passed list will be included as
109109
#' 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`.
111111
#' @param ... Additional arguments passed to methods.
112112
#' @return An `epi_df` object.
113-
#'
113+
#'
114114
#' @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(), ...) {
117117
# Check that we have a data frame
118118
if (!is.data.frame(x)) {
119119
Abort("`x` must be a data frame.")
120120
}
121-
121+
122122
if (!is.list(additional_metadata)) {
123123
Abort("`additional_metadata` must be a list type.")
124124
}
@@ -128,52 +128,55 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
128128

129129
# If geo type is missing, then try to guess it
130130
if (missing(geo_type)) {
131-
geo_type = guess_geo_type(x$geo_value)
131+
geo_type <- guess_geo_type(x$geo_value)
132132
}
133-
133+
134134
# If time type is missing, then try to guess it
135135
if (missing(time_type)) {
136-
time_type = guess_time_type(x$time_value)
136+
time_type <- guess_time_type(x$time_value)
137137
}
138-
138+
139139
# If as_of is missing, then try to guess it
140140
if (missing(as_of)) {
141141
# First check the metadata for an as_of field
142142
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
145145
}
146-
146+
147147
# 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
154158
}
155-
159+
156160
# 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+
163167
# Reorder columns (geo_value, time_value, ...)
164-
if(sum(dim(x)) != 0){
168+
if (sum(dim(x)) != 0) {
165169
cols_to_put_first <- c("geo_value", "time_value")
166170
x <- x[, c(
167171
cols_to_put_first,
168172
# All other columns
169173
names(x)[!(names(x) %in% cols_to_put_first)]
170-
)
171-
]
174+
)]
172175
}
173-
176+
174177
# 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
177180
return(x)
178181
}
179182

@@ -205,77 +208,85 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
205208
#' @return An `epi_df` object.
206209
#'
207210
#' @export
208-
#' @examples
211+
#' @examples
209212
#' # Convert a `tsibble` that has county code as an extra key
210213
#' # Notice that county code should be a character string to preserve any leading zeroes
211-
#'
214+
#'
212215
#' ex1_input <- tibble::tibble(
213216
#' 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+
#' ),
217222
#' 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)),
219225
#' value = 1:length(geo_value) + 0.01 * rnorm(length(geo_value))
220-
#' ) %>%
226+
#' ) %>%
221227
#' tsibble::as_tsibble(index = time_value, key = c(geo_value, county_code))
222-
#'
228+
#'
223229
#' # The `other_keys` metadata (`"county_code"` in this case) is automatically
224230
#' # inferred from the `tsibble`'s `key`:
225231
#' 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+
#'
230236
#' # Dealing with misspecified column names:
231237
#' # Geographical and temporal information must be provided in columns named
232238
#' # `geo_value` and `time_value`; if we start from a data frame with a
233239
#' # different format, it must be converted to use `geo_value` and `time_value`
234240
#' # before calling `as_epi_df`.
235-
#'
241+
#'
236242
#' ex2_input <- tibble::tibble(
237243
#' state = rep(c("ca", "fl", "pa"), each = 3), # misnamed
238244
#' pol = rep(c("blue", "swing", "swing"), each = 3), # extra key
239245
#' 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
241248
#' value = 1:length(state) + 0.01 * rnorm(length(state))
242-
#' )
243-
#'
249+
#' )
250+
#'
244251
#' 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+
#'
254264
#' # Adding additional keys to an `epi_df` object
255-
#'
265+
#'
256266
#' ex3_input <- jhu_csse_county_level_subset %>%
257267
#' 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 %>%
261271
#' tsibble::as_tsibble() %>% # needed to add the additional metadata
262272
#' # add 2 extra keys
263273
#' 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
267278
#' # component of additional_metadata.
268279
#' 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, ...) {
272283
UseMethod("as_epi_df")
273284
}
274285

275286
#' @method as_epi_df epi_df
276287
#' @describeIn as_epi_df Simply returns the `epi_df` object unchanged.
277288
#' @export
278-
as_epi_df.epi_df = function(x, ...) {
289+
as_epi_df.epi_df <- function(x, ...) {
279290
return(x)
280291
}
281292

@@ -289,27 +300,31 @@ as_epi_df.epi_df = function(x, ...) {
289300
#' be used.
290301
#' @importFrom rlang .data
291302
#' @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(), ...) {
294305
# Check that we have geo_value and time_value columns
295306
if (!("geo_value" %in% names(x))) {
296307
Abort("`x` must contain a `geo_value` column.")
297308
}
298309
if (!("time_value" %in% names(x))) {
299310
Abort("`x` must contain a `time_value` column.")
300311
}
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+
)
304317
}
305318

306319
#' @method as_epi_df data.frame
307320
#' @describeIn as_epi_df Works analogously to `as_epi_df.tbl_df()`.
308321
#' @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+
)
313328
}
314329

315330
#' @method as_epi_df tbl_ts
@@ -318,23 +333,26 @@ as_epi_df.data.frame = function(x, geo_type, time_type, as_of,
318333
#' "geo_value") are added to the metadata of the returned object, under the
319334
#' `other_keys` field.
320335
#' @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")
324339
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+
)
327343
}
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+
)
330348
}
331349

332350
#' Test for `epi_df` format
333351
#'
334352
#' @param x An object.
335353
#' @return `TRUE` if the object inherits from `epi_df`.
336-
#'
354+
#'
337355
#' @export
338-
is_epi_df = function(x) {
356+
is_epi_df <- function(x) {
339357
inherits(x, "epi_df")
340358
}

0 commit comments

Comments
 (0)