Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my unit test run successfully in the R console but returns an error with "make test"?

Tags:

unit-testing

r

I am learning how to develop an R package. Everything goes well, thanks to the R manuals and this wiki for RUnit. More precisely, when I launch my unit tests within a new R console, all tests finish successfully:

#rm(list=ls())
library(RUnit)
testSuite <- defineTestSuite("current", "~/src/mypkg/inst/unitTests/")
isValidTestSuite  # returns TRUE
runTestSuite(testSuite)  # returns Number of errors: 0 and Number of failures: 0

However, when I launch them in a terminal, I got one error (the function in question uses the package GenomicRanges that I installed in "~/src/Rlibs"):

$ make test R_LIBS="~/src/Rlibs/"
...
ERROR in test.MyFunction: Error in match(x, table, nomatch = 0L) :
  'match' requires vector arguments

I don't see what is causing this error. I guess you will need more info about the code and the test, but it's not easy because I don't know how to replicate this error on a small example without making a new package just for this. Maybe some of you will have an idea about this error message and give me some hints?

Edit: to help someone to give me a hint on the error, here is the code I wrote for a dummy package. The aim is to find which items of "p" are included within items of "g".

Here is the test:

test.MyFunction <- function(){
  g <- list(c1=data.frame(name=c("g1","g2"), start=c(11,1111),
                  end=c(500,1500), strand=c("+","+"), stringsAsFactors=FALSE))
  p <- list(c1=data.frame(name=c("p1","p2"), strand=c("+","-"),
                   start=c(11,601), end=c(20, 610), stringsAsFactors=FALSE))
  exp <- list(c1=list(g1=c("p1")))  # item "p1" is included in item "g1"
  obs <- MyFunction(g, p)
  checkEquals(obs, exp)
}

And here is the function itself:

MyFunction <- function(g, p){
  res <- lapply(names(g), function(c.name){
    res.c <- list()
    nb.g <- length(g[[c.name]]$name)

    if(length(.find.package("GenomicRanges", quiet=TRUE)) > 0){
      g.ranges <- GRanges(seqnames=Rle(c(c.name), c(nb.g)),
                            ranges=IRanges(g[[c.name]]$start,
                              g[[c.name]]$end, names=g[[c.name]]$name),
                            strand="*")
      p.ranges <- GRanges(seqnames=Rle(c(c.name), nrow(p[[c.name]])),
                             ranges=IRanges(p[[c.name]]$start,
                               p[[c.name]]$end, names=p[[c.name]]$name),
                             strand=p[[c.name]]$strand)
      for(g.name in names(g.ranges)){
        links <- p.ranges %in% g.ranges[names(g.ranges) == g.name]
        if(sum(links) > 0)
          res.c[[g.name]] <- names(p.ranges)[which(links)]
      }
    } else{
      msg <- "can't find package GenomicRanges"
      stop(msg, call.=FALSE)
    }
    res.c
  })

  names(res) <- names(g)
  return(res)
}
like image 420
tflutre Avatar asked Sep 03 '11 02:09

tflutre


People also ask

How do I run a unit test in R?

The easiest way to get started is with usethis. Assuming you're in a package directory, just run usethis::use_test("name") to create a test file, and set up all the other infrastructure you need. If you're using RStudio, press Cmd/Ctrl + Shift + T (or run devtools::test() if not) to run all the tests in a package.

Can we test a method which does not return any value?

However, bear in mind that a method that does not return a value, but does something, relies on side effects and therefore is not really amenable to unit testing, as you cannot test the whole state of a system in a unit test.

How do you test a method in Java?

Each test method should have an "annotation" @ Test . This tells the JUnit test framework that this is an executable test method. To run the tests, select the project with the right-mouse button and click Test. Let's add functionality for adding and subtracting to the test class.


1 Answers

I think this line is your culprit:

links <- p.ranges %in% g.ranges[names(g.ranges) == g.name].

%in% is match, and that is what the error message seems to be reading:

ERROR in test.MyFunction: Error in match(x, table, nomatch = 0L) :
  'match' requires vector arguments

There is something about p.ranges and g.ranges that it doesn't like. I.e., they can't be coerced to vectors OR you're not subsetting properly and the object type is incorrect ([ or [[).

like image 151
Brandon Bertelsen Avatar answered Sep 18 '22 23:09

Brandon Bertelsen