Skip to content

Commit 5571830

Browse files
committed
Refactor add_count functions to improve readability and maintainability by separating the output assignment and sorting logic. Ensure compatibility with dplyr's latest API by using dplyr::n() for counting. Update both default and Seurat-specific implementations.
1 parent 9f63e7b commit 5571830

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

R/dplyr_methods.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,11 +953,12 @@ add_count <- function(x, ..., wt=NULL, sort=FALSE, name=NULL) {
953953
#' @rdname count
954954
add_count.default <- function(x, ..., wt=NULL, sort=FALSE, name=NULL) {
955955
if (is.null(name)) name <- "n"
956-
x %>%
956+
.out <- x %>%
957957
dplyr::group_by(..., .add = TRUE) %>%
958-
dplyr::mutate(!!rlang::sym(name) := if (is.null(wt)) n() else sum(!!enquo(wt), na.rm = TRUE)) %>%
959-
dplyr::ungroup() %>%
960-
{ if (sort) dplyr::arrange(., desc(!!rlang::sym(name))) else . }
958+
dplyr::mutate(!!rlang::sym(name) := if (is.null(wt)) dplyr::n() else sum(!!enquo(wt), na.rm = TRUE)) %>%
959+
dplyr::ungroup()
960+
if (sort) .out <- dplyr::arrange(.out, dplyr::desc(!!rlang::sym(name)))
961+
.out
961962
}
962963

963964
#' @rdname count
@@ -974,14 +975,13 @@ add_count.Seurat <- function(x, ..., wt=NULL, sort=FALSE, name=NULL) {
974975
}
975976

976977
if (is.null(name)) name <- "n"
977-
x@meta.data <-
978-
x %>%
978+
.out <- x %>%
979979
as_tibble %>%
980980
dplyr::group_by(..., .add = TRUE) %>%
981-
dplyr::mutate(!!sym(name) := if (is.null(wt)) n() else sum(!!enquo(wt), na.rm = TRUE)) %>%
982-
dplyr::ungroup() %>%
983-
{ if (sort) dplyr::arrange(., desc(!!sym(name))) else . } %>%
984-
as_meta_data(x)
981+
dplyr::mutate(!!sym(name) := if (is.null(wt)) dplyr::n() else sum(!!enquo(wt), na.rm = TRUE)) %>%
982+
dplyr::ungroup()
983+
if (sort) .out <- dplyr::arrange(.out, dplyr::desc(!!sym(name)))
984+
x@meta.data <- .out %>% as_meta_data(x)
985985

986986
x
987987
}

0 commit comments

Comments
 (0)