Skip to content

Commit 443660a

Browse files
authored
Replace stats::ave() (#6698)
* use vctrs-variant of `stats::ave()` * replace other instances of `stats::ave()` too
1 parent 69e1671 commit 443660a

File tree

4 files changed

+19
-8
lines changed

4 files changed

+19
-8
lines changed

R/facet-.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,14 +1421,14 @@ censor_labels <- function(ranges, layout, labels) {
14211421
)
14221422

14231423
if (!labels$x) {
1424-
xmax <- stats::ave(layout$ROW, layout$COL, FUN = max)
1425-
xmin <- stats::ave(layout$ROW, layout$COL, FUN = min)
1424+
xmax <- vec_ave(layout$ROW, layout$COL, max)
1425+
xmin <- vec_ave(layout$ROW, layout$COL, min)
14261426
draw[which(layout$ROW != xmax), "bottom"] <- FALSE
14271427
draw[which(layout$ROW != xmin), "top"] <- FALSE
14281428
}
14291429
if (!labels$y) {
1430-
ymax <- stats::ave(layout$COL, layout$ROW, FUN = max)
1431-
ymin <- stats::ave(layout$COL, layout$ROW, FUN = min)
1430+
ymax <- vec_ave(layout$COL, layout$ROW, max)
1431+
ymin <- vec_ave(layout$COL, layout$ROW, min)
14321432
draw[which(layout$COL != ymax), "right"] <- FALSE
14331433
draw[which(layout$COL != ymin), "left"] <- FALSE
14341434
}

R/geom-path.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ GeomPath <- ggproto("GeomPath", Geom,
1919
# middle since you expect those to be shown by a break in the line
2020
aesthetics <- c(self$required_aes, self$non_missing_aes)
2121
complete <- stats::complete.cases(data[names(data) %in% aesthetics])
22-
kept <- stats::ave(complete, data$group, data$PANEL, FUN = keep_mid_true)
22+
kept <- vec_ave(complete, interaction(data$group, data$PANEL, drop = TRUE), keep_mid_true)
2323
data <- data[kept, ]
2424

2525
if (!all(kept) && !params$na.rm) {
@@ -48,7 +48,7 @@ GeomPath <- ggproto("GeomPath", Geom,
4848
munched <- coord_munch(coord, data, panel_params)
4949

5050
# Silently drop lines with less than two points, preserving order
51-
rows <- stats::ave(seq_len(nrow(munched)), munched$group, FUN = length)
51+
rows <- vec_ave(seq_len(nrow(munched)), munched$group, length)
5252
munched <- munched[rows >= 2, ]
5353
if (nrow(munched) < 2) return(zeroGrob())
5454

R/stat-sum.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ StatSum <- ggproto(
1515

1616
counts <- count(data, group_by, wt_var = "weight")
1717
counts <- rename(counts, c(freq = "n"))
18-
counts$prop <- stats::ave(counts$n, counts$group, FUN = prop.table)
18+
counts$prop <- vec_ave(counts$n, counts$group, prop.table)
1919
counts
2020
}
2121
)

R/utilities.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ compute_data_size <- function(data, size, default = 0.9,
931931
res <- vapply(res, resolution, FUN.VALUE = numeric(1), ...)
932932
res <- min(res, na.rm = TRUE)
933933
} else if (panels == "by") {
934-
res <- stats::ave(data[[var]], data$PANEL, FUN = function(x) resolution(x, ...))
934+
res <- vec_ave(data[[var]], data$PANEL, function(x) resolution(x, ...))
935935
} else {
936936
res <- resolution(data[[var]], ...)
937937
}
@@ -951,3 +951,14 @@ try_prop <- function(object, name, default = NULL) {
951951
}
952952
S7::prop(object, name)
953953
}
954+
955+
vec_ave <- function(x, by, fn, ...) {
956+
idx <- vec_group_loc(by)$loc
957+
list_unchop(
958+
lapply(
959+
vec_chop(x, indices = idx),
960+
FUN = fn, ...
961+
),
962+
indices = idx
963+
)
964+
}

0 commit comments

Comments
 (0)