Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
46 changes: 46 additions & 0 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -875,3 +875,49 @@ 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
#'
#' @param .data
#'
#' @return
#' @export
#'
#' @examples
#' data(pbmc_small)
#' pbmc_small |> group_split(pbmc_small, groups)
group_split.SingleCellExperiment <- function(.data, var) {


if(any(paste(substitute(var)) == names(colData(.data)))) {
var <- enquo(var)
var_list <- .data |>
as_tibble() |>
select(!!var) |>
unlist(use.names = FALSE)

groups <- unique(var_list)

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

for (n in seq_along(groups)) {
v[[n]] <- .data[, var_list == groups[[n]]]
}

return(v)
}

if(any(paste(substitute(var)) == names(rowData(.data)))) {
var <- enquo(var)
var_list <- .data |>
rowData() |>
as_tibble() |>
select(!!var) |>
unlist(use.names = FALSE)

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

test_that("group_split()", {
fd <- df |>
group_split("groups")
expect_equal(length(fd), length(unique(df$groups)))

fd <- df |>
group_split("vst.variable")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question: what is vst.variable? I cannot find it in the object metadata.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in the rowData. I realise now that code wasn't doing what I thought it did and that it wouldn't have been helpful if it had!

expect_equal(length(fd), length(unique(rowData(df)$vst.variable)))

fd <- df |>
group_split(vst.variable)
expect_equal(length(fd), length(unique(rowData(df)$vst.variable)))
})