Skip to content

Commit d7584be

Browse files
authored
speed up collector() helper (#657)
1 parent 8c4ade4 commit d7584be

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

R/collect.R

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,11 +366,22 @@ collector <- function(x, coll_col = ".predictions") {
366366
} else {
367367
keep_cols <- coll_col
368368
}
369-
x <- dplyr::select(x, dplyr::starts_with("id"), !!!keep_cols)
370-
x <- tidyr::unnest(x, cols = c(dplyr::all_of(coll_col)))
369+
370+
id_cols <- colnames(x)[grepl("id", colnames(x))]
371+
keep_cols <- c(coll_col, id_cols)
372+
x <- x[keep_cols]
373+
coll_col <- x[[coll_col]]
374+
375+
res <-
376+
vctrs::vec_cbind(
377+
vctrs::list_unchop(coll_col),
378+
vctrs::vec_rep_each(x[, id_cols], times = vctrs::list_sizes(coll_col))
379+
)
380+
371381
arrange_cols <- c(".iter", ".config")
372-
arrange_cols <- arrange_cols[(arrange_cols %in% names(x))]
373-
arrange(x, !!!rlang::syms(arrange_cols))
382+
arrange_cols <- arrange_cols[rlang::has_name(res, arrange_cols)]
383+
384+
res <- vctrs::vec_slice(res, vctrs::vec_order(res[arrange_cols]))
374385
}
375386

376387
#' @export

0 commit comments

Comments
 (0)