Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

group_split #97

Merged
merged 23 commits into from
Dec 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ S3method(full_join,SingleCellExperiment)
S3method(ggplot,SingleCellExperiment)
S3method(glimpse,tidySingleCellExperiment)
S3method(group_by,SingleCellExperiment)
S3method(group_split,SingleCellExperiment)
S3method(inner_join,SingleCellExperiment)
S3method(join_transcripts,Seurat)
S3method(join_transcripts,default)
Expand Down Expand Up @@ -73,6 +74,7 @@ importFrom(dplyr,filter)
importFrom(dplyr,full_join)
importFrom(dplyr,group_by)
importFrom(dplyr,group_by_drop_default)
importFrom(dplyr,group_rows)
importFrom(dplyr,group_split)
importFrom(dplyr,inner_join)
importFrom(dplyr,left_join)
Expand Down
38 changes: 38 additions & 0 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -820,3 +820,41 @@ pull.SingleCellExperiment <- function(.data, var=-1, name=NULL, ...) {
as_tibble() %>%
dplyr::pull(var=!!var, name=!!name, ...)
}

#' @name group_split
#' @rdname group_split
#' @inherit dplyr::group_split
#'
#' @examples
#' data(pbmc_small)
#' pbmc_small |> group_split(groups)
#'
#' @importFrom ellipsis check_dots_used
#' @importFrom dplyr group_by
#' @importFrom dplyr group_rows
#' @export
group_split.SingleCellExperiment <- function(.tbl, ..., .keep = TRUE) {

stemangiola marked this conversation as resolved.
Show resolved Hide resolved
var_list <- enquos(...)

group_list <- .tbl |>
as_tibble() |>
dplyr::group_by(!!!var_list)

groups <- group_list |>
dplyr::group_rows()

v <- vector(mode = "list", length = length(groups))

for (i in seq_along(v)) {
v[[i]] <- .tbl[,groups[[i]]]

if(.keep == FALSE) {
v[[i]] <- select(v[[i]], !(!!!var_list))
}
}

v

}

64 changes: 64 additions & 0 deletions man/group_split.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/testthat/Rplots.pdf
Binary file not shown.
30 changes: 30 additions & 0 deletions tests/testthat/test-dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,33 @@ test_that("rowwise()", {
expect_equal(dim(fd), c(ncol(df), 1))
expect_identical(fd[[1]], sapply(df$lys, sum))
})

test_that("group_split() works for one variable", {
fd <- df |>
group_split(groups)
expect_equal(length(fd), length(unique(df$groups)))
})

test_that("group_split() works for combination of variables", {
fd <- df |>
group_split(groups, ident)
expect_equal(length(fd), length(unique(df$groups)) *
length(unique(df$ident)))
})

test_that("group_split() works for one logical statement", {
fd_log <- df |>
group_split(groups=="g1")
fd_var <- df |>
group_split(groups=="g1")
expect_equal(lapply(fd_var, count), lapply(fd_log, count))
})

test_that("group_split() works for two logical statements", {
fd <- df |>
group_split(PC_1>0 & groups=="g1")
fd_counts <- lapply(fd, count)
expect_equal(c(fd_counts[[1]], fd_counts[[2]], use.names = FALSE),
list(75, 5))
})

Loading