Skip to content

Commit ecc9b3a

Browse files
authored
Merge pull request #97 from B0ydT/boyd
group_split
2 parents 8bf5436 + b2a7e3d commit ecc9b3a

File tree

5 files changed

+134
-0
lines changed

5 files changed

+134
-0
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ S3method(full_join,SingleCellExperiment)
1313
S3method(ggplot,SingleCellExperiment)
1414
S3method(glimpse,tidySingleCellExperiment)
1515
S3method(group_by,SingleCellExperiment)
16+
S3method(group_split,SingleCellExperiment)
1617
S3method(inner_join,SingleCellExperiment)
1718
S3method(join_transcripts,Seurat)
1819
S3method(join_transcripts,default)
@@ -73,6 +74,7 @@ importFrom(dplyr,filter)
7374
importFrom(dplyr,full_join)
7475
importFrom(dplyr,group_by)
7576
importFrom(dplyr,group_by_drop_default)
77+
importFrom(dplyr,group_rows)
7678
importFrom(dplyr,group_split)
7779
importFrom(dplyr,inner_join)
7880
importFrom(dplyr,left_join)

R/dplyr_methods.R

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,3 +820,41 @@ pull.SingleCellExperiment <- function(.data, var=-1, name=NULL, ...) {
820820
as_tibble() %>%
821821
dplyr::pull(var=!!var, name=!!name, ...)
822822
}
823+
824+
#' @name group_split
825+
#' @rdname group_split
826+
#' @inherit dplyr::group_split
827+
#'
828+
#' @examples
829+
#' data(pbmc_small)
830+
#' pbmc_small |> group_split(groups)
831+
#'
832+
#' @importFrom ellipsis check_dots_used
833+
#' @importFrom dplyr group_by
834+
#' @importFrom dplyr group_rows
835+
#' @export
836+
group_split.SingleCellExperiment <- function(.tbl, ..., .keep = TRUE) {
837+
838+
var_list <- enquos(...)
839+
840+
group_list <- .tbl |>
841+
as_tibble() |>
842+
dplyr::group_by(!!!var_list)
843+
844+
groups <- group_list |>
845+
dplyr::group_rows()
846+
847+
v <- vector(mode = "list", length = length(groups))
848+
849+
for (i in seq_along(v)) {
850+
v[[i]] <- .tbl[,groups[[i]]]
851+
852+
if(.keep == FALSE) {
853+
v[[i]] <- select(v[[i]], !(!!!var_list))
854+
}
855+
}
856+
857+
v
858+
859+
}
860+

man/group_split.Rd

Lines changed: 64 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/Rplots.pdf

5.65 KB
Binary file not shown.

tests/testthat/test-dplyr_methods.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,33 @@ test_that("rowwise()", {
376376
expect_equal(dim(fd), c(ncol(df), 1))
377377
expect_identical(fd[[1]], sapply(df$lys, sum))
378378
})
379+
380+
test_that("group_split() works for one variable", {
381+
fd <- df |>
382+
group_split(groups)
383+
expect_equal(length(fd), length(unique(df$groups)))
384+
})
385+
386+
test_that("group_split() works for combination of variables", {
387+
fd <- df |>
388+
group_split(groups, ident)
389+
expect_equal(length(fd), length(unique(df$groups)) *
390+
length(unique(df$ident)))
391+
})
392+
393+
test_that("group_split() works for one logical statement", {
394+
fd_log <- df |>
395+
group_split(groups=="g1")
396+
fd_var <- df |>
397+
group_split(groups=="g1")
398+
expect_equal(lapply(fd_var, count), lapply(fd_log, count))
399+
})
400+
401+
test_that("group_split() works for two logical statements", {
402+
fd <- df |>
403+
group_split(PC_1>0 & groups=="g1")
404+
fd_counts <- lapply(fd, count)
405+
expect_equal(c(fd_counts[[1]], fd_counts[[2]], use.names = FALSE),
406+
list(75, 5))
407+
})
408+

0 commit comments

Comments
 (0)