Skip to content

Commit 6a22c4b

Browse files
committed
Use different automatic names for slides on logical columns
1 parent 251ec5e commit 6a22c4b

File tree

4 files changed

+51
-17
lines changed

4 files changed

+51
-17
lines changed

R/slide.R

+19-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,24 @@ 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).
689+
tautology <- function(col) TRUE
687690
f_possibilities <-
688691
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",
692+
~f, ~package, ~namer,
693+
frollmean, "data.table", ~ if (is.logical(.x)) "prop" else "av",
694+
frollsum, "data.table", ~ if (is.logical(.x)) "count" else "sum",
695+
frollapply, "data.table", ~"slide",
696+
slide_sum, "slider", ~ if (is.logical(.x)) "count" else "sum",
697+
slide_prod, "slider", ~"prod",
698+
slide_mean, "slider", ~ if (is.logical(.x)) "prop" else "av",
699+
slide_min, "slider", ~"min",
700+
slide_max, "slider", ~"max",
701+
slide_all, "slider", ~"all",
702+
slide_any, "slider", ~"any",
700703
)
701704
f_info <- f_possibilities %>%
702705
filter(map_lgl(.data$f, ~ identical(.f, .x)))
@@ -780,7 +783,7 @@ epi_slide_opt <- function(
780783
.n = n,
781784
.time_unit_abbr = time_unit_abbr,
782785
.align_abbr = align_abbr,
783-
.f_abbr = f_info$abbr,
786+
.f_abbr = purrr::map_chr(.x[col_names_chr], unwrap(f_info$namer)),
784787
quo_get_env(col_names_quo)
785788
)
786789
.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
@@ -810,8 +810,24 @@ test_that("epi_slide_opt output naming features", {
810810
yearmonthly %>% epi_slide_opt(value, slide_any, .window_size = 3) %>% names(),
811811
c(names(yearmonthly), "value_3many") # not the best name, but super unlikely anyway
812812
)
813+
# * Through forwarding functions:
814+
expect_equal(
815+
# XXX perhaps this should be an auto-naming feature?
816+
yearmonthly %>%
817+
epi_slide_mean(value, .window_size = Inf) %>%
818+
names(),
819+
c(names(yearmonthly), "value_running_prop")
820+
)
821+
expect_equal(
822+
# XXX perhaps this should be an auto-naming feature?
823+
yearmonthly %>%
824+
epi_slide_sum(value, .window_size = Inf) %>%
825+
names(),
826+
c(names(yearmonthly), "value_running_count")
827+
)
813828

814829
# Manual naming:
830+
# * Various combinations of args:
815831
expect_equal(
816832
multi_columns %>% epi_slide_opt(starts_with("value"), slide_sum, .window_size = 7, .suffix = "_s{.n}") %>% names(),
817833
c(names(multi_columns), "value_s7", "value2_s7")
@@ -828,6 +844,15 @@ test_that("epi_slide_opt output naming features", {
828844
multi_columns %>% epi_slide_opt(starts_with("value"), slide_sum, .window_size = 7, .new_col_names = c("slide_value", "sv2")) %>% names(),
829845
c(names(multi_columns), "slide_value", "sv2")
830846
)
847+
# * Through forwarding functions:
848+
expect_equal(
849+
yearmonthly %>% epi_slide_mean(value, .window_size = Inf, .suffix = "_{.f_abbr}") %>% names(),
850+
c(names(yearmonthly), "value_prop")
851+
)
852+
expect_equal(
853+
yearmonthly %>% epi_slide_sum(value, .window_size = Inf, .suffix = "_{.f_abbr}") %>% names(),
854+
c(names(yearmonthly), "value_count")
855+
)
831856

832857
# Validation errors:
833858
# * Wrong sizes:

0 commit comments

Comments
 (0)