Skip to content

Commit

Permalink
Allow negative subsetting of columns in cytoframe/cytoset
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobpwagner committed Dec 11, 2019
1 parent de1b072 commit fd10e74
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
11 changes: 9 additions & 2 deletions R/cytoframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
}
Expand Down
8 changes: 8 additions & 0 deletions R/cytoset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/testthat/test-cytoset.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down

0 comments on commit fd10e74

Please sign in to comment.