|
59 | 59 | #'
|
60 | 60 | #' @importFrom data.table between key
|
61 | 61 | #' @export
|
62 |
| -epix_as_of <- function(x, version = NULL, min_time_value = -Inf, all_versions = FALSE, |
| 62 | +epix_as_of <- function(x, version, min_time_value = -Inf, all_versions = FALSE, |
63 | 63 | max_version = deprecated()) {
|
64 | 64 | assert_class(x, "epi_archive")
|
65 |
| - version <- version %||% x$versions_end |
66 | 65 |
|
67 | 66 | if (lifecycle::is_present(max_version)) {
|
68 | 67 | lifecycle::deprecate_warn("0.8.1", "epix_as_of(max_version =)", "epix_as_of(version =)")
|
@@ -114,26 +113,79 @@ epix_as_of <- function(x, version = NULL, min_time_value = -Inf, all_versions =
|
114 | 113 | if (all_versions) {
|
115 | 114 | # epi_archive is copied into result, so we can modify result directly
|
116 | 115 | result <- epix_truncate_versions_after(x, version)
|
117 |
| - result$DT <- result$DT[time_value >= .min_time_value, ] # nolint: object_usage_linter |
| 116 | + if (!identical(.min_time_value, -Inf)) { |
| 117 | + # See below for why we need this branch. |
| 118 | + result$DT <- result$DT[time_value >= .min_time_value, ] # nolint: object_usage_linter |
| 119 | + } |
118 | 120 | return(result)
|
119 | 121 | }
|
120 | 122 |
|
121 | 123 | # Make sure to use data.table ways of filtering and selecting
|
122 |
| - as_of_epi_df <- x$DT[time_value >= .min_time_value & version <= .version, ] %>% # nolint: object_usage_linter |
123 |
| - unique( |
124 |
| - by = c("geo_value", "time_value", other_keys), |
125 |
| - fromLast = TRUE |
126 |
| - ) %>% |
| 124 | + as_of_epi_df <- if (identical(.min_time_value, -Inf)) { |
| 125 | + # This branch is needed for `epix_as_of` to work with `yearmonth` time type |
| 126 | + # to avoid time_value > .min_time_value, which is NA for `yearmonth`. |
| 127 | + x$DT[version <= .version, ] |
| 128 | + } else { |
| 129 | + x$DT[time_value >= .min_time_value & version <= .version, ] |
| 130 | + } # nolint: object_usage_linter |
| 131 | + as_of_epi_df <- as_of_epi_df %>% |
| 132 | + unique(by = c("geo_value", "time_value", other_keys), fromLast = TRUE) %>% |
127 | 133 | tibble::as_tibble() %>%
|
128 | 134 | dplyr::select(-"version") %>%
|
129 |
| - as_epi_df( |
130 |
| - as_of = version, |
131 |
| - other_keys = other_keys |
132 |
| - ) |
| 135 | + as_epi_df(as_of = .version, other_keys = other_keys) |
133 | 136 |
|
134 | 137 | return(as_of_epi_df)
|
135 | 138 | }
|
136 | 139 |
|
| 140 | +#' Get the latest snapshot from an `epi_archive` object, using |
| 141 | +#' the current time value for the time type. |
| 142 | +#' |
| 143 | +#' @param x An `epi_archive` object |
| 144 | +#' @return The latest snapshot from an `epi_archive` object |
| 145 | +#' @export |
| 146 | +epix_as_of_now <- function(x) { |
| 147 | + current_time_value <- current_time_value(x) |
| 148 | + x %>% |
| 149 | + set_versions_end(current_time_value) %>% |
| 150 | + epix_as_of(current_time_value) |
| 151 | +} |
| 152 | + |
| 153 | +#' Get the latest time value for an `epi_archive` object |
| 154 | +#' |
| 155 | +#' @param x An `epi_archive` object |
| 156 | +#' @return The latest time value for an `epi_archive` object |
| 157 | +#' @importFrom tsibble yearmonth |
| 158 | +#' @noRd |
| 159 | +#' @keywords internal |
| 160 | +current_time_value <- function(x) { |
| 161 | + if (x$time_type == "day") { |
| 162 | + Sys.Date() |
| 163 | + } else if (x$time_type == "week") { |
| 164 | + max(seq(from = max(x$DT$time_value), to = Sys.Date(), by = "week")) |
| 165 | + } else if (x$time_type == "yearmonth") { |
| 166 | + yearmonth(Sys.Date()) |
| 167 | + } else if (x$time_type == "integer") { |
| 168 | + cli_abort("Unable to determine the latest time value for an integer time type. Use `epix_as_of` instead.") |
| 169 | + } else { |
| 170 | + cli_abort("Unsupported time type in column `{time_value_arg}`, with class {.code {class(time_value)}}. |
| 171 | + Time-related functionality may have unexpected behavior. |
| 172 | + ", class = "epiprocess__epix_as_of_now_unsupported_time_type") |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +#' Set the `versions_end` attribute of an `epi_archive` object |
| 177 | +#' |
| 178 | +#' An escape hatch for epix_as_of, which does not allow version > |
| 179 | +#' `$versions_end`. |
| 180 | +#' |
| 181 | +#' @param x An `epi_archive` object |
| 182 | +#' @param versions_end The new `versions_end` value |
| 183 | +#' @return An `epi_archive` object with the updated `versions_end` attribute |
| 184 | +#' @export |
| 185 | +set_versions_end <- function(x, versions_end) { |
| 186 | + x$versions_end <- versions_end |
| 187 | + x |
| 188 | +} |
137 | 189 |
|
138 | 190 | #' Fill `epi_archive` unobserved history
|
139 | 191 | #'
|
@@ -880,10 +932,13 @@ epix_slide.epi_archive <- function(
|
880 | 932 | #' @noRd
|
881 | 933 | epix_slide_versions_default <- function(ea) {
|
882 | 934 | versions_with_updates <- c(ea$DT$version, ea$versions_end)
|
883 |
| - tidyr::full_seq(versions_with_updates, guess_period(versions_with_updates)) |
| 935 | + if (ea$time_type == "yearmonth") { |
| 936 | + min(versions_with_updates) + seq(0, max(versions_with_updates) - min(versions_with_updates), by = 1) |
| 937 | + } else { |
| 938 | + tidyr::full_seq(versions_with_updates, guess_period(versions_with_updates)) |
| 939 | + } |
884 | 940 | }
|
885 | 941 |
|
886 |
| - |
887 | 942 | #' Filter an `epi_archive` object to keep only older versions
|
888 | 943 | #'
|
889 | 944 | #' Generates a filtered `epi_archive` from an `epi_archive` object, keeping
|
|
0 commit comments