Skip to content

Commit 6ffa6a2

Browse files
committed
Actually turn epi_slide_opt into S3 method
1 parent 7e36241 commit 6ffa6a2

6 files changed

+80
-42
lines changed

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ Collate:
9797
'correlation.R'
9898
'epi_df.R'
9999
'epi_df_forbidden_methods.R'
100+
'epi_slide_opt_archive.R'
100101
'epi_slide_opt_edf.R'
101102
'epiprocess-package.R'
102-
'epix_epi_slide_opt.R'
103103
'group_by_epi_df_methods.R'
104104
'methods-epi_archive.R'
105105
'grouped_epi_archive.R'

NAMESPACE

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ S3method(dplyr_col_modify,col_modify_recorder_df)
2727
S3method(dplyr_col_modify,epi_df)
2828
S3method(dplyr_reconstruct,epi_df)
2929
S3method(dplyr_row_slice,epi_df)
30-
S3method(epix_epi_slide_opt,epi_archive)
31-
S3method(epix_epi_slide_opt,grouped_epi_archive)
30+
S3method(epi_slide_opt,epi_archive)
31+
S3method(epi_slide_opt,epi_df)
32+
S3method(epi_slide_opt,grouped_epi_archive)
3233
S3method(epix_slide,epi_archive)
3334
S3method(epix_slide,grouped_epi_archive)
3435
S3method(epix_truncate_versions_after,epi_archive)
@@ -75,7 +76,6 @@ export(epi_slide_mean)
7576
export(epi_slide_opt)
7677
export(epi_slide_sum)
7778
export(epix_as_of)
78-
export(epix_epi_slide_opt)
7979
export(epix_fill_through_version)
8080
export(epix_merge)
8181
export(epix_slide)

R/epix_epi_slide_opt.R R/epi_slide_opt_archive.R

+17-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#' Core operation of `epix_epi_slide_opt` for a single epikey's history
1+
#' Core operation of `epi_slide_opt.epi_archive` for a single epikey's history
22
#'
33
#' @param updates tibble with two columns: `version` and `subtbl`; `subtbl` is a
44
#' list of tibbles, each with a `time_value` column and measurement columns.
@@ -41,10 +41,10 @@
4141
#' tidyr::nest(.by = version, .key = "subtbl")
4242
#'
4343
#' updates %>%
44-
#' epix_epi_slide_opt_one_epikey("value", data.table::frollmean, "data.table", 1L, 0L, "day", "slide_value")
44+
#' epi_slide_opt_one_epikey("value", data.table::frollmean, "data.table", 1L, 0L, "day", "slide_value")
4545
#'
4646
#' @keywords internal
47-
epix_epi_slide_opt_one_epikey <- function(updates, in_colnames, f_dots_baked, f_from_package, before, after, time_type, out_colnames) {
47+
epi_slide_opt_archive_one_epikey <- function(updates, in_colnames, f_dots_baked, f_from_package, before, after, time_type, out_colnames) {
4848
# TODO check for col name clobbering
4949
unit_step <- epiprocess:::unit_time_delta(time_type)
5050
prev_inp_snapshot <- NULL
@@ -124,21 +124,9 @@ epix_epi_slide_opt_one_epikey <- function(updates, in_colnames, f_dots_baked, f_
124124
result
125125
}
126126

127-
# TODO just make this an epi_slide_opt impl?
128-
127+
#' @method epi_slide_opt grouped_epi_archive
129128
#' @export
130-
epix_epi_slide_opt <-
131-
function(.x, .col_names, .f, ...,
132-
.window_size = NULL, .align = c("right", "center", "left"),
133-
.prefix = NULL, .suffix = NULL, .new_col_names = NULL # ,
134-
## .ref_time_values = NULL, .all_rows = FALSE
135-
) {
136-
UseMethod("epix_epi_slide_opt")
137-
}
138-
139-
#' @method epix_epi_slide_opt grouped_epi_archive
140-
#' @export
141-
epix_epi_slide_opt.grouped_epi_archive <- function(.x, ...) {
129+
epi_slide_opt.grouped_epi_archive <- function(.x, ...) {
142130
assert_set_equal(
143131
group_vars(.x),
144132
key_colnames(.x, exclude = c("time_value", "version"))
@@ -147,16 +135,17 @@ epix_epi_slide_opt.grouped_epi_archive <- function(.x, ...) {
147135
orig_drop <- .x$private$drop
148136
.x %>%
149137
ungroup() %>%
150-
epix_epi_slide_opt(...) %>%
138+
epi_slide_opt(...) %>%
151139
group_by(pick(all_of(orig_group_vars)), .drop = orig_drop)
152140
}
153-
#' @method epix_epi_slide_opt epi_archive
141+
142+
#' @method epi_slide_opt epi_archive
154143
#' @export
155-
epix_epi_slide_opt.epi_archive <-
144+
epi_slide_opt.epi_archive <-
156145
function(.x, .col_names, .f, ...,
157146
.window_size = NULL, .align = c("right", "center", "left"),
158147
.prefix = NULL, .suffix = NULL, .new_col_names = NULL,
159-
## , .ref_time_values = NULL, .all_rows = FALSE
148+
.ref_time_values = NULL, .all_rows = FALSE,
160149
.progress = FALSE) {
161150
# Extract metadata:
162151
time_type <- .x$time_type
@@ -175,6 +164,12 @@ epix_epi_slide_opt.epi_archive <-
175164
col_names_quo <- enquo(.col_names)
176165
names_info <- across_ish_names_info(.x$DT, time_type, col_names_quo, .f_info$namer, .window_size, .align, .prefix, .suffix, .new_col_names)
177166
window_args <- get_before_after_from_window(.window_size, .align, time_type)
167+
if (!is.null(.ref_time_values)) {
168+
cli_abort("epi_slide.epi_archive does not support the `.ref_time_values` argument")
169+
}
170+
if (!identical(.all_rows, FALSE)) {
171+
cli_abort("epi_slide.epi_archive does not support the `.all_rows` argument")
172+
}
178173
assert(
179174
checkmate::check_logical(.progress, any.missing = FALSE, len = 1L, names = "unnamed"),
180175
checkmate::check_string(.progress)
@@ -196,7 +191,7 @@ epix_epi_slide_opt.epi_archive <-
196191
nest(.by = version, .key = "subtbl") %>%
197192
arrange(version)
198193
# TODO move nesting inside the helper?
199-
res <- epix_epi_slide_opt_one_epikey(group_updates, names_info$input_col_names, .f_dots_baked, .f_info$from_package, window_args$before, window_args$after, time_type, names_info$output_col_names) %>%
194+
res <- epi_slide_opt_archive_one_epikey(group_updates, names_info$input_col_names, .f_dots_baked, .f_info$from_package, window_args$before, window_args$after, time_type, names_info$output_col_names) %>%
200195
list_rbind()
201196
if (use_progress) cli::cli_progress_update(id = progress_bar_id)
202197
res

R/epi_slide_opt_edf.R

+31-5
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,23 @@ across_ish_names_info <- function(.x, time_type, col_names_quo, .f_namer, .windo
159159

160160
#' Optimized slide functions for common cases
161161
#'
162-
#' @description `epi_slide_opt` allows sliding an n-timestep [data.table::froll]
163-
#' or [slider::summary-slide] function over variables in an `epi_df` object.
164-
#' These functions tend to be much faster than `epi_slide()`. See
165-
#' `vignette("epi_df")` for more examples.
162+
#' @description
163+
#'
164+
#' `epi_slide_opt` calculates n-time-step rolling means&sums,
165+
#' cumulative/"running" means&sums, or other operations supported by
166+
#' [`data.table::froll`] or [`slider::summary-slide`] functions.
167+
#'
168+
#' * On `epi_df`s, it will take care of looping over `geo_value`s, temporarily
169+
#' filling in time gaps with `NA`s and other work needed to ensure there are
170+
#' exactly n consecutive time steps per computation, and has some other
171+
#' convenience features. See `vignette("epi_df")` for more examples.
172+
#'
173+
#' * On `epi_archive`s, it will calculate the version history for these slide
174+
#' computations and combine it with the version history for the rest of the
175+
#' columns.
176+
#'
177+
#' This function tends to be much faster than using `epi_slide()` and
178+
#' `epix_slide()` directly.
166179
#'
167180
#' @template basic-slide-params
168181
#' @param .col_names <[`tidy-select`][dplyr_tidy_select]> An unquoted column
@@ -279,13 +292,26 @@ across_ish_names_info <- function(.x, time_type, col_names_quo, .f_namer, .windo
279292
#' .suffix = "_{.n}{.time_unit_abbr}_median"
280293
#' ) %>%
281294
#' print(n = 40)
295+
#'
296+
#' # You can calculate entire version histories for the derived signals by
297+
#' # calling `epi_slide_opt()` on an `epi_archive`:
298+
#' case_death_rate_archive %>%
299+
#' epi_slide_mean(case_rate, .window_size = 14)
300+
#'
301+
#' @export
282302
epi_slide_opt <- function(
283303
.x, .col_names, .f, ...,
284304
.window_size = NULL, .align = c("right", "center", "left"),
285305
.prefix = NULL, .suffix = NULL, .new_col_names = NULL,
286306
.ref_time_values = NULL, .all_rows = FALSE) {
287-
assert_class(.x, "epi_df")
307+
UseMethod("epi_slide_opt")
308+
}
288309

310+
#' @export
311+
epi_slide_opt.epi_df <- function(.x, .col_names, .f, ...,
312+
.window_size = NULL, .align = c("right", "center", "left"),
313+
.prefix = NULL, .suffix = NULL, .new_col_names = NULL,
314+
.ref_time_values = NULL, .all_rows = FALSE) {
289315
# Deprecated argument handling
290316
provided_args <- rlang::call_args_names(rlang::call_match())
291317
if (any(purrr::map_lgl(provided_args, ~ .x %in% c("x", "col_names", "f", "ref_time_values", "all_rows")))) {

man/epi_slide_opt.Rd

+21-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/epix_epi_slide_opt_one_epikey.Rd man/epi_slide_opt_archive_one_epikey.Rd

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)