From fd10e742addfe73c92861bccd9dd0b6394efec9b Mon Sep 17 00:00:00 2001 From: Jacob Wagner Date: Wed, 11 Dec 2019 10:56:50 -0800 Subject: [PATCH] Allow negative subsetting of columns in cytoframe/cytoset --- R/cytoframe.R | 11 +++++++++-- R/cytoset.R | 8 ++++++++ tests/testthat/test-cytoset.R | 5 +++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/R/cytoframe.R b/R/cytoframe.R index 7d422a52..34431a35 100644 --- a/R/cytoframe.R +++ b/R/cytoframe.R @@ -427,8 +427,15 @@ setMethod("[", stop(msg, call.=FALSE) } - if(is.numeric(j)||is.integer(j)) - subset_cytoframe_by_cols(fr@pointer, j - 1) + if(is.numeric(j)||is.integer(j)){ + if(any(j < 0)){ + if(!all(j <= 0)){ + stop("Cannot mix positive and negative subscripts") + } + j <- (1:length(colnames(x)))[j] + } + subset_cytoframe_by_cols(fr@pointer, j - 1) + } else stop("invalid j index!") } diff --git a/R/cytoset.R b/R/cytoset.R index 57dbd5e5..2e6cc7ba 100644 --- a/R/cytoset.R +++ b/R/cytoset.R @@ -685,6 +685,14 @@ setMethod("[", if(missing(j)) j <- NULL + if(is.numeric(j)||is.integer(j)){ + if(any(j < 0)){ + if(!all(j <= 0)){ + stop("Cannot mix positive and negative subscripts") + } + j <- (1:length(colnames(x)))[j] + } + } x <- copy_view(x) subset_cytoset(x@pointer, i, j) x diff --git a/tests/testthat/test-cytoset.R b/tests/testthat/test-cytoset.R index 31bf56f0..c51de9aa 100644 --- a/tests/testthat/test-cytoset.R +++ b/tests/testthat/test-cytoset.R @@ -86,6 +86,11 @@ test_that("[", { expect_equal(sampleNames(cs1[-c(3,7,11)]), sampleNames(cs1)[-c(3,7,11)]) expect_error(cs1[c(-3, 7, -11)], "Cannot mix positive and negative subscripts") + #Test negative subsetting of columns for cytoset and cytoframe + expect_equal(colnames(cs1[,-c(3,5)]), colnames(cs1)[-c(3,5)]) + cf1 <- cs1[[1]] + expect_equal(colnames(cf1[,-c(2,4)]), colnames(cf1)[-c(2,4)]) + }) test_that("subset", {