Skip to content

Commit 872dc36

Browse files
committed
perf(epi_slide): bind_{rows,cols} -> vec_{rbind,cbind} + adj if nec
1 parent be927c1 commit 872dc36

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,11 @@ importFrom(tsibble,as_tsibble)
242242
importFrom(utils,capture.output)
243243
importFrom(utils,tail)
244244
importFrom(vctrs,vec_cast)
245+
importFrom(vctrs,vec_cbind)
245246
importFrom(vctrs,vec_data)
246247
importFrom(vctrs,vec_duplicate_any)
247248
importFrom(vctrs,vec_equal)
248249
importFrom(vctrs,vec_order)
250+
importFrom(vctrs,vec_rbind)
249251
importFrom(vctrs,vec_size)
250252
importFrom(vctrs,vec_sort)

R/epiprocess-package.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@
2727
#' @importFrom rlang %||%
2828
#' @importFrom rlang is_bare_integerish
2929
#' @importFrom tools toTitleCase
30+
#' @importFrom vctrs vec_cbind
3031
#' @importFrom vctrs vec_data
3132
#' @importFrom vctrs vec_equal
3233
#' @importFrom vctrs vec_order
34+
#' @importFrom vctrs vec_rbind
3335
#' @importFrom vctrs vec_sort
3436
## usethis namespace: end
3537
NULL

R/slide.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
#' determined the time window for the current computation.
6666
#'
6767
#' @importFrom lubridate days weeks
68-
#' @importFrom dplyr bind_rows group_map group_vars filter select
68+
#' @importFrom dplyr group_map group_vars filter select
6969
#' @importFrom rlang .data .env !! enquos sym env missing_arg
7070
#' @export
7171
#' @seealso [`epi_slide_opt`] for optimized slide functions
@@ -309,7 +309,7 @@ epi_slide <- function(
309309
...,
310310
.keep = TRUE
311311
) %>%
312-
bind_rows() %>%
312+
{vec_rbind(!!!.)} %>%
313313
`[`(.$.real, names(.) != ".real") %>%
314314
arrange_col_canonical() %>%
315315
group_by(!!!.x_orig_groups)
@@ -341,9 +341,9 @@ epi_slide_one_group <- function(
341341
# time values, padding on the left and right as needed.
342342
all_dates <- .date_seq_list$all_dates
343343
missing_times <- all_dates[!(all_dates %in% .data_group$time_value)]
344-
.data_group <- bind_rows(
345-
.data_group,
346-
dplyr::bind_cols(
344+
.data_group <- reclass(vec_rbind(
345+
.data_group, # (epi_df; epi_slide uses .keep = TRUE)
346+
vec_cbind( # (tibble -> vec_rbind produces tibble)
347347
.group_key,
348348
tibble(
349349
time_value = c(
@@ -353,17 +353,19 @@ epi_slide_one_group <- function(
353353
), .real = FALSE
354354
)
355355
)
356-
) %>%
356+
# we should be adding time values of the same time_type (and shouldn't be
357+
# introducing duplicate epikeytime values); we can reclass without checks:
358+
), attr(.data_group, "metadata")) %>%
357359
`[`(vec_order(.$time_value), )
358360

359361
# If the data group does not contain any of the reference time values, return
360-
# the original .data_group without slide columns and let bind_rows at the end
362+
# the original .data_group without slide columns and let vec_rbind at the end
361363
# of group_modify handle filling the empty data frame with NA values.
362364
if (length(available_ref_time_values) == 0L) {
363365
if (.all_rows) {
364366
return(.data_group)
365367
}
366-
return(.data_group %>% filter(FALSE))
368+
return(.data_group[0, ])
367369
}
368370

369371
# Get stateful function that tracks ref_time_value per group and sends it to
@@ -608,7 +610,7 @@ get_before_after_from_window <- function(window_size, align, time_type) {
608610
#' - `{.f_abbr}` will be a character vector containing a short abbreviation
609611
#' for `.f` factoring in the input column type(s) for `.col_names`
610612
#'
611-
#' @importFrom dplyr bind_rows mutate %>% arrange tibble select all_of
613+
#' @importFrom dplyr mutate %>% arrange tibble select all_of
612614
#' @importFrom rlang enquo expr_label caller_arg quo_get_env
613615
#' @importFrom tidyselect eval_select
614616
#' @importFrom glue glue
@@ -898,8 +900,8 @@ epi_slide_opt <- function(
898900
# `frollmean` requires a full window to compute a result. Add NA values
899901
# to beginning and end of the group so that we get results for the
900902
# first `before` and last `after` elements.
901-
.data_group <- bind_rows(
902-
.data_group,
903+
.data_group <- vec_rbind(
904+
.data_group, # (tibble; epi_slide_opt uses .keep = FALSE)
903905
tibble(time_value = c(missing_times, pad_early_dates, pad_late_dates), .real = FALSE)
904906
) %>%
905907
`[`(vec_order(.$time_value), )

0 commit comments

Comments
 (0)