Skip to content

Commit

Permalink
Add matrixData initializer to set correct number of rows on annotCols…
Browse files Browse the repository at this point in the history
…; Add test for descriptions + add/remove columns workaround
  • Loading branch information
Jan Rudolph committed Jun 13, 2018
1 parent c778d00 commit ae49bbe
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: PerseusR
Title: Perseus R Interop
Version: 0.2.0
Version: 0.2.1
Author: Jan Rudolph <[email protected]>
Maintainer: Jan Rudolph <[email protected]>
Description: Provides utility functions for Perseus and R interop.
Expand Down
15 changes: 15 additions & 0 deletions R/matrixData.R
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ matrixData <- function(...) {
methods::new("matrixData", ...)
}

#' matrixData initializer
#' @description Initializes the annotCols data frame to have the
#' same number of rows as the main data. This might not be the
#' cleanest solution.
setMethod(initialize, "matrixData", function(.Object, ...) {
args <- list(...)
if ("main" %in% names(args) && !("annotCols" %in% names(args))) {
main <- args[['main']]
args[["annotCols"]] <- data.frame(matrix(nrow=nrow(main), ncol=0))
}
args[['.Object']] <- .Object
do.call(callNextMethod, args)
})


getNames <- function(x) {c(colnames(x@main), colnames(x@annotCols))}
#TODO: check if it would be better to have a list returned with one element
#having the col names and the other the row names
Expand Down
14 changes: 14 additions & 0 deletions tests/testthat/testMatrixData.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,17 @@ test_that('the column names span main columns and annotation columns',{
cn <- names(df)
expect_equal(cn, c('a','b','b'))
})

test_that('you can add simple annotation columns to an existing matrix data', {
df <- matrixData(main=data.frame(a=1:3, b=6:8))
annotCols(df)['test'] <- 'a'
expect_equal(annotCols(df)[['test']], rep('a', 3))
})

test_that('you should be able to add new columns regardless of the existance of descriptions', {
df <- matrixData(main=data.frame(a=1:3, b=6:8), description=c('a','b'))
warning("TODO: test uses workaround!")
description(df) <- character(0) # TODO: remove this line, test should still pass, then remove warning
main(df)['c'] <- 9:11
expect_equal(main(df)[['c']], 9:11)
})

0 comments on commit ae49bbe

Please sign in to comment.