Skip to content

Commit 054f93f

Browse files
committed
Fix .by argument of summarise() to work (r-spatial#2207)
1 parent 732525f commit 054f93f

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

R/tidyverse.R

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -290,35 +290,35 @@ summarise.sf <- function(.data, ..., .dots, do_union = TRUE, is_coverage = FALSE
290290
precision = st_precision(.data)
291291
crs = st_crs(.data)
292292
geom = st_geometry(.data)
293+
294+
if (!requireNamespace("dplyr", quietly = TRUE))
295+
stop("dplyr required: install that first") # nocov
293296
class(.data) = setdiff(class(.data), "sf")
294-
ret = NextMethod()
295-
if (!missing(do_union))
296-
ret$do_union = NULL
297-
if (!missing(is_coverage))
298-
ret$is_coverage = NULL
297+
.data$.rows = vctrs::vec_seq_along(.data)
298+
ret = dplyr::summarise(
299+
.data = .data,
300+
...,
301+
.rows = list(.rows)
302+
)
303+
.rows = ret$.rows
304+
ret = ret[names(ret) != ".rows"]
299305

300306
if (! any(sapply(ret, inherits, what = "sfc"))) {
301-
geom = if (inherits(.data, "grouped_df") || inherits(.data, "grouped_dt")) {
302-
if (!requireNamespace("dplyr", quietly = TRUE))
303-
stop("dplyr required: install that first") # nocov
304-
i = dplyr::group_indices(.data)
305-
# geom = st_geometry(.data)
307+
geom = if (nrow(ret) > 1) {
306308
geom = if (do_union)
307-
lapply(sort(unique(i)), function(x) {
308-
if (x == 1)
309-
st_union(geom[i == x], is_coverage = is_coverage)
309+
lapply(.rows, function(x) {
310+
if (1 %in% x)
311+
st_union(geom[x], is_coverage = is_coverage)
310312
else
311-
suppressMessages(st_union(geom[i == x], is_coverage = is_coverage))
313+
suppressMessages(st_union(geom[x], is_coverage = is_coverage))
312314
})
313315
else
314-
lapply(sort(unique(i)), function(x) st_combine(geom[i == x]))
316+
lapply(.rows, function(x) st_combine(geom[x]))
315317
geom = unlist(geom, recursive = FALSE)
316318
if (is.null(geom))
317319
geom = list() #676 #nocov
318320
do.call(st_sfc, c(geom, crs = list(crs), precision = precision))
319321
} else { # single group:
320-
if (nrow(ret) > 1)
321-
stop(paste0("when using .by, also add across(", sf_column, ", st_union) as argument")) # https://github.com/r-spatial/sf/issues/2207
322322
if (do_union)
323323
st_union(geom, is_coverage = is_coverage)
324324
else

0 commit comments

Comments
 (0)