Skip to content

Commit b9bac78

Browse files
committed
Fix names<-.epi_df, introduce failing test for renaming in select
1 parent ac5f369 commit b9bac78

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

R/epi_df.R

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,9 @@ new_epi_df = function(x = tibble::tibble(), geo_type, time_type, as_of,
122122
if (!is.list(additional_metadata)) {
123123
Abort("`additional_metadata` must be a list type.")
124124
}
125+
if (is.null(additional_metadata[["other_keys"]])) {
126+
additional_metadata[["other_keys"]] <- character(0L)
127+
}
125128

126129
# If geo type is missing, then try to guess it
127130
if (missing(geo_type)) {
@@ -334,4 +337,4 @@ as_epi_df.tbl_ts = function(x, geo_type, time_type, as_of,
334337
#' @export
335338
is_epi_df = function(x) {
336339
inherits(x, "epi_df")
337-
}
340+
}

R/group_by_epi_df_methods.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#' @export
33
select.epi_df <- function(.data, ...) {
44
selected <- NextMethod(.data)
5-
return(dplyr_reconstruct(selected, .data))
5+
return (dplyr_reconstruct(selected, .data))
66
}
77

88
# others to consider:

R/methods-epi_df.R

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,16 @@ dplyr_row_slice.epi_df = function(data, i, ...) {
180180
#' @export
181181
`names<-.epi_df` = function(x, value) {
182182
old_names = names(x)
183-
old_other_keys = attributes(x)$metadata$other_keys
183+
old_other_keys = attr(x, "metadata")[["other_keys"]]
184184
result = NextMethod()
185-
attributes(x)$metadata$other_keys <- value[match(old_other_keys, old_names)]
185+
new_other_keys_almost <- value[match(old_other_keys, old_names)]
186+
attr(result, "metadata")[["other_keys"]] <-
187+
# patch until we require `other_keys` to be `chr`; match NULL-ness of input `other_keys`:
188+
# if (length(new_other_keys_almost) == 0L) NULL
189+
# if (is.null(old_other_keys)) NULL
190+
# else new_other_keys_almost
191+
new_other_keys_almost
192+
# decay to non-`epi_df` if needed:
186193
dplyr::dplyr_reconstruct(result, result)
187194
}
188195

tests/testthat/test-methods-epi_df.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,17 @@ test_that("Metadata and grouping are dropped by `as_tibble`", {
124124
!any(c("metadata", "groups") %in% names(attributes(grouped_converted)))
125125
)
126126
})
127+
128+
test_that("Renaming columns gives appropriate colnames and metadata", {
129+
edf <- tibble::tibble(geo_value = 1, time_value = 1, age = 1, value = 1) %>%
130+
as_epi_df(additional_metadata = list(other_keys = "age"))
131+
renamed_edf1 <- edf %>%
132+
`[`(c("geo_value", "time_value", "age", "value")) %>%
133+
`names<-`(c("geo_value", "time_value", "age_group", "value"))
134+
expect_identical(names(renamed_edf1), c("geo_value", "time_value", "age_group", "value"))
135+
expect_identical(attr(renamed_edf1, "metadata")$other_keys, c("age_group"))
136+
renamed_edf2 <- edf %>%
137+
as_epi_df(additional_metadata = list(other_keys = "age")) %>%
138+
select(geo_value, time_value, age_group = age, value)
139+
expect_identical(renamed_edf1, renamed_edf2)
140+
})

0 commit comments

Comments
 (0)