Skip to content

Commit

Permalink
Merge pull request #55 from stemangiola/add_count-drop-generics
Browse files Browse the repository at this point in the history
Add count drop generics
  • Loading branch information
stemangiola authored Feb 13, 2023
2 parents 1cd396d + 4ab0f70 commit 92cc7f7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 136 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: tidySingleCellExperiment
Title: Brings SingleCellExperiment to the Tidyverse
Version: 1.9.2
Version: 1.9.3
Authors@R: c(person("Stefano", "Mangiola", email = "[email protected]",
role = c("aut", "cre")) )
Description: tidySingleCellExperiment is an adapter that abstracts the 'SingleCellExperiment' container
Expand Down
3 changes: 1 addition & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
# Generated by roxygen2: do not edit by hand

S3method(add_count,SingleCellExperiment)
S3method(add_count,default)
S3method(arrange,SingleCellExperiment)
S3method(as_tibble,SingleCellExperiment)
S3method(bind_cols,SingleCellExperiment)
S3method(bind_cols,default)
S3method(bind_rows,SingleCellExperiment)
S3method(bind_rows,default)
S3method(count,SingleCellExperiment)
S3method(count,default)
S3method(distinct,SingleCellExperiment)
S3method(extract,SingleCellExperiment)
S3method(filter,SingleCellExperiment)
Expand Down Expand Up @@ -87,6 +85,7 @@ importFrom(SummarizedExperiment,"colData<-")
importFrom(SummarizedExperiment,assays)
importFrom(SummarizedExperiment,colData)
importFrom(cli,cat_line)
importFrom(dplyr,add_count)
importFrom(dplyr,arrange)
importFrom(dplyr,count)
importFrom(dplyr,distinct)
Expand Down
60 changes: 24 additions & 36 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ sample_frac.SingleCellExperiment <- function(tbl, size=1, replace=FALSE,

#' Count observations by group
#'
#' @importFrom dplyr count
#'
#' @description
#' `count()` lets you quickly count the unique values of one or more variables:
Expand Down Expand Up @@ -1311,30 +1312,19 @@ sample_frac.SingleCellExperiment <- function(tbl, size=1, replace=FALSE,
#' An object of the same type as `.data`. `count()` and `add_count()`
#' group transiently, so the output has the same groups as the input.
#' @export
#'
#' @rdname dplyr-methods
#' @name count
#'
#' @examples
#'
#'
#' `%>%` <- magrittr::`%>%`
#' pbmc_small %>%
#'
#' count(groups)
count <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .drop=group_by_drop_default(x)) {
UseMethod("count")
}
NULL

#' @export
count.default <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .drop=group_by_drop_default(x)) {
if (!missing(...)) {
out <- dplyr::group_by(x, ..., .add=TRUE, .drop=.drop)
}
else {
out <- x
}
out <- dplyr::tally(out, wt=!!enquo(wt), sort=sort, name=name)
if (is.data.frame(x)) {
out <- dplyr::dplyr_reconstruct(out, x)
}
out
}
#' @export
count.SingleCellExperiment <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .drop=group_by_drop_default(x)) {
message(data_frame_returned_message)
Expand All @@ -1352,23 +1342,19 @@ count.SingleCellExperiment <- function(x, ..., wt=NULL, sort=FALSE, name=NULL, .
dplyr::count(..., wt=!!enquo(wt), sort=sort, name=name, .drop=.drop)
}

#' @export
#' @rdname count
add_count <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = group_by_drop_default(x)) {
UseMethod("add_count")
}

#' @export
#' @rdname count
add_count.default <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = group_by_drop_default(x)) {

dplyr::add_count(x=x, ..., wt = !!enquo(wt), sort = sort, name = name, .drop = .drop)

}
#'
#'
#' @importFrom dplyr add_count
#'
#' @name add_count
#'
#' @rdname dplyr-methods
NULL

#' @export
#' @rdname count
add_count.SingleCellExperiment <- function(x, ..., wt = NULL, sort = FALSE, name = NULL, .drop = group_by_drop_default(x)) {
add_count.SingleCellExperiment <- function(x, ..., wt = NULL, sort = FALSE, name = NULL) {

# Deprecation of special column names
if(is_sample_feature_deprecated_used(
Expand All @@ -1378,16 +1364,18 @@ add_count.SingleCellExperiment <- function(x, ..., wt = NULL, sort = FALSE, name
x= ping_old_special_column_into_metadata(x)
}

colData(x) =
x %>%
as_tibble %>%
dplyr::add_count(..., wt = !!enquo(wt), sort = sort, name = name, .drop = .drop) %>%
as_meta_data(x)
colData(x) =
x %>%
as_tibble %>%
dplyr::add_count(..., wt = !!enquo(wt), sort = sort, name = name) %>%
as_meta_data(x)

x
x

}



#' Extract a single column
#'
#'
Expand Down
92 changes: 0 additions & 92 deletions man/count.Rd

This file was deleted.

43 changes: 38 additions & 5 deletions man/dplyr-methods.Rd

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

7 changes: 7 additions & 0 deletions tests/testthat/test-dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,10 @@ test_that("count", {
nrow() %>%
expect_equal(2)
})

test_that("add count", {
pbmc_small %>%
add_count(groups) %>%
nrow() %>%
expect_equal(230)
})

0 comments on commit 92cc7f7

Please sign in to comment.