Skip to content

Commit 9669773

Browse files
committed
Use different automatic names for slides on logical columns
1 parent fab1c08 commit 9669773

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

R/slide.R

+18-16
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,8 @@ get_before_after_from_window <- function(window_size, align, time_type) {
587587
#' `time_type` of `.x`
588588
#' - `{.align_abbr}` will be `""` if `.align` is the default of `"right"`;
589589
#' otherwise, it will be the first letter of `.align`
590-
#' - `{.f_abbr}` will be a short string based on what `.f`
590+
#' - `{.f_abbr}` will be a character vector containing a short abbreviation
591+
#' for `.f` factoring in the input column type(s) for `.col_names`
591592
#'
592593
#' @importFrom dplyr bind_rows mutate %>% arrange tibble select all_of
593594
#' @importFrom rlang enquo expr_label caller_arg quo_get_env
@@ -681,22 +682,23 @@ epi_slide_opt <- function(
681682
col_names_chr <- names(.x)[pos]
682683

683684
# Check that slide function `.f` is one of those short-listed from
684-
# `data.table` and `slider` (or a function that has the exact same
685-
# definition, e.g. if the function has been reexported or defined
686-
# locally).
685+
# `data.table` and `slider` (or a function that has the exact same definition,
686+
# e.g. if the function has been reexported or defined locally). Extract some
687+
# metadata. `namer` will be mapped over columns (.x will be a column, not the
688+
# entire edf).
687689
f_possibilities <-
688690
tibble::tribble(
689-
~f, ~package, ~abbr,
690-
frollmean, "data.table", "av",
691-
frollsum, "data.table", "sum",
692-
frollapply, "data.table", "slide",
693-
slide_sum, "slider", "sum",
694-
slide_prod, "slider", "prod",
695-
slide_mean, "slider", "av",
696-
slide_min, "slider", "min",
697-
slide_max, "slider", "max",
698-
slide_all, "slider", "all",
699-
slide_any, "slider", "any",
691+
~f, ~package, ~namer,
692+
frollmean, "data.table", ~ if (is.logical(.x)) "prop" else "av",
693+
frollsum, "data.table", ~ if (is.logical(.x)) "count" else "sum",
694+
frollapply, "data.table", ~ "slide",
695+
slide_sum, "slider", ~ if (is.logical(.x)) "count" else "sum",
696+
slide_prod, "slider", ~ "prod",
697+
slide_mean, "slider", ~ if (is.logical(.x)) "prop" else "av",
698+
slide_min, "slider", ~ "min",
699+
slide_max, "slider", ~ "max",
700+
slide_all, "slider", ~ "all",
701+
slide_any, "slider", ~ "any",
700702
)
701703
f_info <- f_possibilities %>%
702704
filter(map_lgl(.data$f, ~ identical(.f, .x)))
@@ -780,7 +782,7 @@ epi_slide_opt <- function(
780782
.n = n,
781783
.time_unit_abbr = time_unit_abbr,
782784
.align_abbr = align_abbr,
783-
.f_abbr = f_info$abbr,
785+
.f_abbr = purrr::map_chr(.x[col_names_chr], unwrap(f_info$namer)),
784786
quo_get_env(col_names_quo)
785787
)
786788
.new_col_names <- unclass(

R/utils.R

+5
Original file line numberDiff line numberDiff line change
@@ -1174,3 +1174,8 @@ time_type_unit_abbr <- function(time_type) {
11741174
}
11751175
maybe_unit_abbr
11761176
}
1177+
1178+
unwrap <- function(x) {
1179+
checkmate::assert_list(x, len = 1L, names = "unnamed")
1180+
x[[1L]]
1181+
}

man/epi_slide_opt.Rd

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

tests/testthat/test-epi_slide.R

+25
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,24 @@ test_that("epi_slide_opt output naming features", {
812812
yearmonthly %>% epi_slide_opt(value, slide_any, .window_size = 3) %>% names(),
813813
c(names(yearmonthly), "value_3many") # not the best name, but super unlikely anyway
814814
)
815+
# * Through forwarding functions:
816+
expect_equal(
817+
# XXX perhaps this should be an auto-naming feature?
818+
yearmonthly %>%
819+
epi_slide_mean(value, .window_size = Inf) %>%
820+
names(),
821+
c(names(yearmonthly), "value_running_prop")
822+
)
823+
expect_equal(
824+
# XXX perhaps this should be an auto-naming feature?
825+
yearmonthly %>%
826+
epi_slide_sum(value, .window_size = Inf) %>%
827+
names(),
828+
c(names(yearmonthly), "value_running_count")
829+
)
815830

816831
# Manual naming:
832+
# * Various combinations of args:
817833
expect_equal(
818834
multi_columns %>%
819835
epi_slide_opt(starts_with("value"), slide_sum, .window_size = 7, .suffix = "_s{.n}") %>%
@@ -838,6 +854,15 @@ test_that("epi_slide_opt output naming features", {
838854
names(),
839855
c(names(multi_columns), "slide_value", "sv2")
840856
)
857+
# * Through forwarding functions:
858+
expect_equal(
859+
yearmonthly %>% epi_slide_mean(value, .window_size = Inf, .suffix = "_{.f_abbr}") %>% names(),
860+
c(names(yearmonthly), "value_prop")
861+
)
862+
expect_equal(
863+
yearmonthly %>% epi_slide_sum(value, .window_size = Inf, .suffix = "_{.f_abbr}") %>% names(),
864+
c(names(yearmonthly), "value_count")
865+
)
841866

842867
# Validation errors:
843868
# * Wrong sizes:

0 commit comments

Comments
 (0)