Hi Everyone,
I ran into an error that I am trying to solve where my unit tests work from the R console via BiocGenerics:::testPackage("PackageName") but don't work when using R CMD check PackageName from the console. Below is a description of a small R package that can duplicate the error. Since I am not very creative I just call the package ErrorReproducer :(.
R -> ErrorReproducer.R
ErrorReproducer <- setClass("ErrorReproducer", slots = c(expressionData = "matrix", clinicalData = "data.frame"), validity = function(object) { rowExpr <- rownames(object@expressionData) rowClinical <- rownames(object@clinicalData) if(length(intersect(rowExpr, rowClinical)) == 0) { return("There are no shared samples between the expression data and clinical data.") } return(TRUE) } )
inst -> unitTests -> test_ErrorReproducer.R
library(RUnit) runTests <- function() { test_ErrorReproducer() } test_ErrorReproducer = function () { #Setup some data to test the class generator function testMatrix <- as.matrix(mtcars[, 1:4]) testDF <- mtcars[, 5:8] myObject <- ErrorReproducer(expressionData = testMatrix, clinicalData = testDF) rownames(testDF) <- 1:dim(testDF)[1] #No shared observations in the gene expression data (we do not need this check for the clinical data since it is a data frame) test1 <- try(ErrorReproducer(expressionData = testMatrix, clinicalData = testDF), silent = TRUE) checkEquals(test1[1], "Error in validObject(.Object) : \n invalid class “ErrorReproducer” object: There are no shared samples between the expression data and clinical data.\n") }
tests -> runTests.R
require("ErrorReproducer") || stop("unable to load ErrorReproducer package") require("BiocGenerics") || stop("unable to load BiocGenerics package") BiocGenerics:::testPackage("ErrorReproducer")
NAMESPACE
exportClasses(ErrorReproducer) export(ErrorReproducer) import(methods)
DESCRIPTION
Package: ErrorReproducer Type: Package Title: Error Reproducer Date: 2015-5-11 Version: 0.99.1 Author: Human Maintainer: Human <human@species.org> Depends: R (>= 3.1.0), methods Suggests: BiocGenerics, RUnit Description: Error Test License: GPL (>= 2)
Using the code above, when I install and test the package within R using BiocGenerics:::testPackage("ErrorReproducer") the test returns no errors.
RUNIT TEST PROTOCOL -- Mon May 11 15:19:01 2015 *********************************************** Number of test functions: 1 Number of errors: 0 Number of failures: 0 1 Test Suite : ErrorReproducer RUnit Tests - 1 test function, 0 errors, 0 failures Number of test functions: 1 Number of errors: 0 Number of failures: 0
If however I do the same from the command line using R CMD check ErrorReproducer I get the following:
* checking tests ... Running ‘runTests.R’ ERROR Running the tests in ‘tests/runTests.R’ failed. Last 13 lines of output: ErrorReproducer RUnit Tests - 1 test function, 0 errors, 1 failure FAILURE in test_ErrorReproducer: Error in checkEquals(test1[1], "Error in validObject(.Object) : \n invalid class “ErrorReproducer” object: There are no shared samples between the expression data and clinical data.\n") : 1 string mismatch Test files with failing tests test_ErrorReproducer.R test_ErrorReproducer Error in BiocGenerics:::testPackage("ErrorReproducer") : unit tests failed for package ErrorReproducer Execution halted
Does anyone know what I might be missing here or misunderstanding? I just want to be able to use unit tests with my package, so having them fail from the terminal but not from the console is somewhat confusing.
Thanks in advance to anyone who is willing to help me with this.
Wade
> sessionInfo() R version 3.1.3 (2015-03-09) Platform: x86_64-unknown-linux-gnu (64-bit) Running under: Ubuntu 14.04.2 LTS locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C LC_TIME=en_US.UTF-8 [4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base other attached packages: [1] RUnit_0.4.28 ErrorReproducer_0.99.1 loaded via a namespace (and not attached): [1] BiocGenerics_0.12.1 parallel_3.1.3 tools_3.1.3
The unit test fails for me in both scenarios on R-3.2.0 (which you should be using if the package you are developing is intended for submission to Bioconductor).
A tip to make the BiocGenerics run act more like the R CMD check run is to look at the 'check' script in
$R_HOME/bin
. On my system it ends with this line:That tells us that R runs with different settings under R CMD check. Note that the locale is different which can impact tests involving sort order. Also R_DEFAULT_PACKAGES is unset meaning that no packages are attached by default when R starts up. So now start R like this:
And run
Does it still succeed?