Skip to content

Commit

Permalink
refactor(epix_slide): before defaults to Inf, remove time_step
Browse files Browse the repository at this point in the history
  • Loading branch information
dshemetov committed Jun 21, 2024
1 parent 01d77aa commit 0464f24
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 56 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ importFrom(checkmate,check_atomic)
importFrom(checkmate,check_data_frame)
importFrom(checkmate,check_names)
importFrom(checkmate,expect_class)
importFrom(checkmate,testInt)
importFrom(checkmate,test_set_equal)
importFrom(checkmate,test_subset)
importFrom(checkmate,vname)
Expand Down
1 change: 1 addition & 0 deletions R/epiprocess.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' assert_logical assert_list assert_character assert_class
#' assert_int assert_numeric check_data_frame vname check_atomic
#' anyInfinite test_subset test_set_equal checkInt expect_class
#' testInt
#' @importFrom cli cli_abort cli_warn
#' @importFrom rlang %||%
#' @name epiprocess
Expand Down
30 changes: 13 additions & 17 deletions R/grouped_epi_archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,16 @@ ungroup.grouped_epi_archive <- function(x, ...) {
#' @importFrom rlang !! !!! enquo quo_is_missing enquos is_quosure sym syms
#' env missing_arg
#' @export
epix_slide.grouped_epi_archive <- function(x, f, ..., before, ref_time_values,
time_step, new_col_name = "slide_value",
as_list_col = FALSE, names_sep = "_",
all_versions = FALSE) {
epix_slide.grouped_epi_archive <- function(
x,
f,
...,
before = Inf,
ref_time_values = NULL,
new_col_name = "slide_value",
as_list_col = FALSE,
names_sep = "_",
all_versions = FALSE) {
# Perform some deprecated argument checks without using `<param> =
# deprecated()` in the function signature, because they are from
# early development versions and much more likely to be clutter than
Expand All @@ -231,7 +237,7 @@ epix_slide.grouped_epi_archive <- function(x, f, ..., before, ref_time_values,
", class = "epiprocess__epix_slide_all_rows_parameter_deprecated")
}

if (missing(ref_time_values)) {
if (is.null(ref_time_values)) {
ref_time_values <- epix_slide_ref_time_values_default(x$private$ungrouped)
} else {
assert_numeric(ref_time_values, min.len = 1L, null.ok = FALSE, any.missing = FALSE)
Expand All @@ -247,19 +253,9 @@ epix_slide.grouped_epi_archive <- function(x, f, ..., before, ref_time_values,
}

# Validate and pre-process `before`:
if (missing(before)) {
cli_abort("`before` is required (and must be passed by name);
if you did not want to apply a sliding window but rather
to map `epix_as_of` and `f` across various `ref_time_values`,
pass a large `before` value (e.g., if time steps are days,
`before=365000`).")
if (!(anyInfinite(before) || testInt(before, lower = 0L, null.ok = FALSE, na.ok = FALSE))) {
cli_abort("`before` must be a non-negative integer or Inf.")
}
before <- vctrs::vec_cast(before, integer())
assert_int(before, lower = 0L, null.ok = FALSE, na.ok = FALSE)

# If a custom time step is specified, then redefine units

if (!missing(time_step)) before <- time_step(before)

# Symbolize column name
new_col <- sym(new_col_name)
Expand Down
22 changes: 13 additions & 9 deletions R/methods-epi_archive.R
Original file line number Diff line number Diff line change
Expand Up @@ -790,9 +790,8 @@ epix_slide <- function(
x,
f,
...,
before,
ref_time_values,
time_step,
before = Inf,
ref_time_values = NULL,
new_col_name = "slide_value",
as_list_col = FALSE,
names_sep = "_",
Expand All @@ -803,19 +802,24 @@ epix_slide <- function(

#' @rdname epix_slide
#' @export
epix_slide.epi_archive <- function(x, f, ..., before, ref_time_values,
time_step, new_col_name = "slide_value",
as_list_col = FALSE, names_sep = "_",
all_versions = FALSE) {
epix_slide.epi_archive <- function(
x,
f,
...,
before = Inf,
ref_time_values = NULL,
new_col_name = "slide_value",
as_list_col = FALSE,
names_sep = "_",
all_versions = FALSE) {
# For an "ungrouped" slide, treat all rows as belonging to one big
# group (group by 0 vars), like `dplyr::summarize`, and let the
# resulting `grouped_epi_archive` handle the slide:
epix_slide(
group_by(x),
f,
...,
before = before, ref_time_values = ref_time_values,
time_step = time_step, new_col_name = new_col_name,
before = before, ref_time_values = ref_time_values, new_col_name = new_col_name,
as_list_col = as_list_col, names_sep = names_sep,
all_versions = all_versions
) %>%
Expand Down
27 changes: 12 additions & 15 deletions man/epix_slide.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 3 additions & 15 deletions tests/testthat/test-epix_slide.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,30 +174,18 @@ test_that("epix_slide works as intended with `as_list_col=TRUE`", {
})

test_that("epix_slide `before` validation works", {
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary)),
"`before` is required"
)
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary), before = NA),
"Assertion on 'before' failed: May not be NA"
"`before` must be a non-negative integer or Inf."
)
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary), before = -1),
"Assertion on 'before' failed: Element 1 is not >= 0"
"`before` must be a non-negative integer or Inf."
)
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary), before = 1.5),
regexp = "before",
class = "vctrs_error_incompatible_type"
)
# We might want to allow this at some point (issue #219):
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary), before = Inf),
regexp = "before",
class = "vctrs_error_incompatible_type"
"`before` must be a non-negative integer or Inf."
)
expect_error(xx %>% epix_slide(f = ~ sum(.x$binary)), "`before` is required")
# These `before` values should be accepted:
expect_error(
xx %>% epix_slide(f = ~ sum(.x$binary), before = 0),
Expand Down

0 comments on commit 0464f24

Please sign in to comment.