Skip to content

Commit 4b60c79

Browse files
committed
feat: support group2 as a number in clone selectors
1 parent 5bdb2ba commit 4b60c79

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

R/clonalutils.R

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ screp_subset <- function(screp, subset) {
163163
#' @importFrom dplyr across
164164
#' @param envtype The type of environment to use. It can be "tidy" for dplyr verbs, "scplotter" for scplotter functions, or "unknown" if the context cannot be determined.
165165
#' @param out The output data frame to be processed.
166+
#' @param x The name to be backtick-quoted
166167
#' @keywords internal
167168
.get_envtype <- function() {
168169
# Returns:
@@ -277,6 +278,23 @@ screp_subset <- function(screp, subset) {
277278
.return_what(out, id, return_ids)
278279
}
279280

281+
#' @rdname clone_selector_utils
282+
#' @keywords internal
283+
.bquote <- function(x) {
284+
if (is.character(x)) {
285+
if (!suppressWarnings(is.na(as.numeric(x)))) {
286+
return(x) # if x is a number, return it as is
287+
}
288+
if (grepl("`", x)) {
289+
return(x) # already quoted
290+
} else {
291+
return(paste0("`", x, "`")) # backtick-quote the name
292+
}
293+
} else {
294+
return(x)
295+
}
296+
}
297+
280298
#' Helper functions to select clones based on various criteria
281299
#'
282300
#' @name CloneSelectors
@@ -466,9 +484,9 @@ uniq <- function(group1, group2, ..., groups = NULL, data = NULL, id = NULL, in_
466484
groups <- unique(c(env$facet_by, env$split_by))
467485
}
468486
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
469-
expr <- paste0("`", group1, "` > 0 & `", group2, "` == 0")
487+
expr <- paste0(.bquote(group1), " > 0 & ", .bquote(group2), " == 0")
470488
for (g in other_groups) {
471-
expr <- paste0(expr, " & `", g, "` == 0")
489+
expr <- paste0(expr, " & ", .bquote(g), " == 0")
472490
}
473491
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
474492
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -497,9 +515,9 @@ shared <- function(group1, group2, ..., groups = NULL, data = NULL, id = NULL, i
497515
groups <- unique(c(env$facet_by, env$split_by))
498516
}
499517
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
500-
expr <- paste0("`", group1, "` > 0 & `", group2, "` > 0")
518+
expr <- paste0(.bquote(group1), " > 0 & ", .bquote(group2), " > 0")
501519
for (g in other_groups) {
502-
expr <- paste0(expr, " & `", g, "` > 0")
520+
expr <- paste0(expr, " & ", .bquote(g), " > 0")
503521
}
504522
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
505523
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -527,9 +545,9 @@ gt <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
527545
groups <- unique(c(env$facet_by, env$split_by))
528546
}
529547
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
530-
expr <- paste0("`", group1, "` > `", group2, "`")
548+
expr <- paste0(.bquote(group1), " > ", .bquote(group2))
531549
if (!include_zeros) {
532-
expr <- paste0("`", group2, "` > 0 & ", expr)
550+
expr <- paste0(.bquote(group2), " > 0 & ", expr)
533551
}
534552
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
535553
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -557,9 +575,9 @@ ge <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
557575
groups <- unique(c(env$facet_by, env$split_by))
558576
}
559577
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
560-
expr <- paste0("`", group1, "` >= `", group2, "`")
578+
expr <- paste0(.bquote(group1), " >= ", .bquote(group2))
561579
if (!include_zeros) {
562-
expr <- paste0("`", group2, "` > 0 & ", expr)
580+
expr <- paste0(.bquote(group2), " > 0 & ", expr)
563581
}
564582
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
565583
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -587,9 +605,9 @@ lt <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
587605
groups <- unique(c(env$facet_by, env$split_by))
588606
}
589607
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
590-
expr <- paste0("`", group1, "` < `", group2, "`")
608+
expr <- paste0(.bquote(group1), " < ", .bquote(group2))
591609
if (!include_zeros) {
592-
expr <- paste0("`", group1, "` > 0 & ", expr)
610+
expr <- paste0(.bquote(group1), " > 0 & ", expr)
593611
}
594612
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
595613
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -617,9 +635,9 @@ le <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
617635
groups <- unique(c(env$facet_by, env$split_by))
618636
}
619637
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
620-
expr <- paste0("`", group1, "` <= `", group2, "`")
638+
expr <- paste0(.bquote(group1), " <= ", .bquote(group2))
621639
if (!include_zeros) {
622-
expr <- paste0("`", group1, "` > 0 & ", expr)
640+
expr <- paste0(.bquote(group1), " > 0 & ", expr)
623641
}
624642
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
625643
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
@@ -647,7 +665,7 @@ eq <- function(group1, group2, groups = NULL, data = NULL, id = NULL, in_form =
647665
groups <- unique(c(env$facet_by, env$split_by))
648666
}
649667
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
650-
expr <- paste0("`", group1, "` == `", group2, "`")
668+
expr <- paste0(.bquote(group1), " == ", .bquote(group2))
651669
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
652670
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)
653671
}
@@ -674,9 +692,9 @@ ne <- function(group1, group2, include_zeros = TRUE, groups = NULL, data = NULL,
674692
groups <- unique(c(env$facet_by, env$split_by))
675693
}
676694
id <- id %||% ifelse(envtype == "tidy", "CTaa", "CloneID")
677-
expr <- paste0("`", group1, "` != `", group2, "`")
695+
expr <- paste0(.bquote(group1), " != ", .bquote(group2))
678696
if (!include_zeros) {
679-
expr <- paste0("`", group1, "` > 0 & `", group2, "` > 0 & ", expr)
697+
expr <- paste0(.bquote(group1), " > 0 & ", .bquote(group2), " > 0 & ", expr)
680698
}
681699
stopifnot("`groups` must be provided when `in_form` is 'long'." = !is.null(groups) || in_form == "wide")
682700
sel(expr, groups, data, id = id, in_form = in_form, return_ids = return_ids)

man/clone_selector_utils.Rd

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

tests/testthat/test-clone_selectors_compare.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ test_that("gt()/ge()/lt()/le() returns selected elements", {
55
g1 = c(10, 20, 30, 40, 50),
66
g2 = c(5, 15, 35, 40, 30)
77
)
8+
# group2 works with numbers
9+
result <- gt(g1, 40, data = df, in_form = "wide", return_ids = FALSE)
10+
expect_equal(result$x, c("E"))
11+
812
result <- gt(g1, g2, data = df, in_form = "wide", return_ids = FALSE)
913
expect_equal(result$x, c("A", "B", "E"))
1014

0 commit comments

Comments
 (0)