Skip to content

Commit

Permalink
fix slice_min slice_max tibble input
Browse files Browse the repository at this point in the history
  • Loading branch information
wvictor14 committed Aug 21, 2023
1 parent 7420193 commit 7cca3f8
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 36 deletions.
54 changes: 39 additions & 15 deletions R/dplyr_methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -908,23 +908,24 @@ slice.Seurat <- function (.data, ..., .by = NULL, .preserve = FALSE)
#' This must evaluate to a vector of non-negative numbers the same length as
#' the input. Weights are automatically standardised to sum to 1.
#'
#' @examples
#' # slice_sample() allows you to random select with or without replacement
#' pbmc_small |> slice_sample(n = 5)
#' @examples
#'
#' # slice_sample() allows you to random select with or without replacement
#' pbmc_small |> slice_sample(n = 5)
#'
#' # if using replacement, and duplicate cells are returned, a tibble will be
#' # returned because duplicate cells cannot exist in Seurat objects
#' pbmc_small |> slice_sample(n = 1, replace = TRUE) # returns Seurat
#' pbmc_small |> slice_sample(n = 100, replace = TRUE) # returns tibble
#' # if using replacement, and duplicate cells are returned, a tibble will be
#' # returned because duplicate cells cannot exist in Seurat objects
#' pbmc_small |> slice_sample(n = 1, replace = TRUE) # returns Seurat
#' pbmc_small |> slice_sample(n = 100, replace = TRUE) # returns tibble
#'
#' # weight by a variable
#' pbmc_small |> slice_sample(n = 5, weight_by = nCount_RNA)
#' # weight by a variable
#' pbmc_small |> slice_sample(n = 5, weight_by = nCount_RNA)
#'
#' # sample by group
#' pbmc_small |> slice_sample(n = 5, by = groups)
#' # sample by group
#' pbmc_small |> slice_sample(n = 5, by = groups)
#'
#' # sample using proportions
#' pbmc_small |> slice_sample(prop = 0.10)
#' # sample using proportions
#' pbmc_small |> slice_sample(prop = 0.10)
#'
NULL

Expand Down Expand Up @@ -1039,8 +1040,11 @@ NULL

#' @export
slice_min.Seurat <- function(.data, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE) {

order_by_vars <- return_args(!!enexpr(order_by))

idx <- .data[[]] |>
select(-everything(), {{order_by}}, {{ by }}) |>
select(-everything(), !!!order_by_vars, {{ by }}) |>
rowid_to_column(var = 'row_number___') |>
slice_min(
order_by = {{ order_by }}, ..., n = n, prop = prop, by = {{ by }},
Expand All @@ -1056,6 +1060,7 @@ slice_min.Seurat <- function(.data, order_by, ..., n, prop, by = NULL, with_ties
#' @rdname dplyr-methods
#' @name slice_max
#' @importFrom dplyr slice_max
#' @export
#' @examples
#'
#' # Rows with minimum and maximum values of a metadata variable
Expand All @@ -1064,8 +1069,11 @@ NULL

#' @export
slice_max.Seurat <- function(.data, order_by, ..., n, prop, by = NULL, with_ties = TRUE, na_rm = FALSE) {

order_by_vars <- return_args(!!enexpr(order_by))

idx <- .data[[]] |>
select(-everything(), {{order_by}}, {{ by }}) |>
select(-everything(), !!!order_by_vars, {{ by }}) |>
rowid_to_column(var = 'row_number___') |>
slice_max(
order_by = {{ order_by }}, ..., n = n, prop = prop, by = {{ by }},
Expand All @@ -1078,6 +1086,22 @@ slice_max.Seurat <- function(.data, order_by, ..., n, prop, by = NULL, with_ties
new_obj
}

#' returns variables from an expression
#' @importFrom rlang enexpr
#' @return list of symbols
return_args <- function(args){

args_expr <- enexpr(args)

if(length(args_expr) == 1) {
args_vars <- as.list(args_expr)
} else {
args_vars <- as.list(args_expr)[-1]
}

args_vars
}

#' Subset columns using their names and types
#'
#' @description
Expand Down
44 changes: 23 additions & 21 deletions man/dplyr-methods.Rd

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

14 changes: 14 additions & 0 deletions man/return_args.Rd

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

0 comments on commit 7cca3f8

Please sign in to comment.