From e502421e9a4628b2b01bb2274280dd7a816cdba9 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 13 Oct 2025 11:14:19 +0200 Subject: [PATCH 1/2] use vctrs-variant of `stats::ave()` --- R/utilities.R | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/R/utilities.R b/R/utilities.R index 37000121c0..ec5225d1ba 100644 --- a/R/utilities.R +++ b/R/utilities.R @@ -907,7 +907,7 @@ compute_data_size <- function(data, size, default = 0.9, res <- vapply(res, resolution, FUN.VALUE = numeric(1), ...) res <- min(res, na.rm = TRUE) } else if (panels == "by") { - res <- stats::ave(data[[var]], data$PANEL, FUN = function(x) resolution(x, ...)) + res <- vec_ave(data[[var]], data$PANEL, function(x) resolution(x, ...)) } else { res <- resolution(data[[var]], ...) } @@ -927,3 +927,14 @@ try_prop <- function(object, name, default = NULL) { } S7::prop(object, name) } + +vec_ave <- function(x, by, fn, ...) { + idx <- vec_group_loc(by)$loc + list_unchop( + lapply( + vec_chop(x, indices = idx), + FUN = fn, ... + ), + indices = idx + ) +} From 76c03a7692fc58bbd693a2781b64e38664603791 Mon Sep 17 00:00:00 2001 From: Teun van den Brand Date: Mon, 13 Oct 2025 11:38:35 +0200 Subject: [PATCH 2/2] replace other instances of `stats::ave()` too --- R/facet-.R | 8 ++++---- R/geom-path.R | 4 ++-- R/stat-sum.R | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/R/facet-.R b/R/facet-.R index e4a010d612..52192902be 100644 --- a/R/facet-.R +++ b/R/facet-.R @@ -1415,14 +1415,14 @@ censor_labels <- function(ranges, layout, labels) { ) if (!labels$x) { - xmax <- stats::ave(layout$ROW, layout$COL, FUN = max) - xmin <- stats::ave(layout$ROW, layout$COL, FUN = min) + xmax <- vec_ave(layout$ROW, layout$COL, max) + xmin <- vec_ave(layout$ROW, layout$COL, min) draw[which(layout$ROW != xmax), "bottom"] <- FALSE draw[which(layout$ROW != xmin), "top"] <- FALSE } if (!labels$y) { - ymax <- stats::ave(layout$COL, layout$ROW, FUN = max) - ymin <- stats::ave(layout$COL, layout$ROW, FUN = min) + ymax <- vec_ave(layout$COL, layout$ROW, max) + ymin <- vec_ave(layout$COL, layout$ROW, min) draw[which(layout$COL != ymax), "right"] <- FALSE draw[which(layout$COL != ymin), "left"] <- FALSE } diff --git a/R/geom-path.R b/R/geom-path.R index 9688e9e2c2..78788620ac 100644 --- a/R/geom-path.R +++ b/R/geom-path.R @@ -19,7 +19,7 @@ GeomPath <- ggproto("GeomPath", Geom, # middle since you expect those to be shown by a break in the line aesthetics <- c(self$required_aes, self$non_missing_aes) complete <- stats::complete.cases(data[names(data) %in% aesthetics]) - kept <- stats::ave(complete, data$group, FUN = keep_mid_true) + kept <- vec_ave(complete, data$group, keep_mid_true) data <- data[kept, ] if (!all(kept) && !params$na.rm) { @@ -48,7 +48,7 @@ GeomPath <- ggproto("GeomPath", Geom, munched <- coord_munch(coord, data, panel_params) # Silently drop lines with less than two points, preserving order - rows <- stats::ave(seq_len(nrow(munched)), munched$group, FUN = length) + rows <- vec_ave(seq_len(nrow(munched)), munched$group, length) munched <- munched[rows >= 2, ] if (nrow(munched) < 2) return(zeroGrob()) diff --git a/R/stat-sum.R b/R/stat-sum.R index c764cbc14f..6be4dbefe9 100644 --- a/R/stat-sum.R +++ b/R/stat-sum.R @@ -15,7 +15,7 @@ StatSum <- ggproto( counts <- count(data, group_by, wt_var = "weight") counts <- rename(counts, c(freq = "n")) - counts$prop <- stats::ave(counts$n, counts$group, FUN = prop.table) + counts$prop <- vec_ave(counts$n, counts$group, prop.table) counts } )