我正在學習如何開發一個R包。一切順利,感謝RUnit的R手冊和this wiki。更確切地說,當我一個新的R控制檯內啓動我的單元測試,所有測試成功完成:爲什麼我的單元測試在R控制檯中成功運行,但是返回「make test」錯誤?
#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
然而,當我啓動它們在終端,我得到了一個錯誤(有問題的函數使用包我安裝在「〜/ src/Rlibs」中):
$ make test R_LIBS="~/src/Rlibs/"
...
ERROR in test.MyFunction: Error in match(x, table, nomatch = 0L) :
'match' requires vector arguments
我看不到是什麼原因導致了這個錯誤。我想你需要更多關於代碼和測試的信息,但這並不容易,因爲我不知道如何在一個小例子上覆制這個錯誤,而不用爲此創建一個新的包。也許你們中的一些人會對這個錯誤信息有所瞭解並給我一些提示?
編輯:幫助某人給我提示錯誤,這是我爲虛擬包編寫的代碼。目的是找出「g」項中包含哪些「p」項。
下面是測試:
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)
}
這裏是函數本身:
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)
}
我不知道問題是什麼,但它可能是由'.Rprofile'中的某些內容引起的。你可以嘗試運行'R --vanilla',看看它的行爲是否有所不同。 – Owen
@Owen我試過了,但它也不起作用。事實上,如果我像這樣啓動它,我甚至不能使用--vanilla,因爲R需要至少讀取我的〜/ .Renviron以查找GenomicRanges包(通過env變量R_LIBS)。 – tflutre
你能告訴我們什麼類型的對象是由'GRanges()'返回的嗎?即str(p.ranges)',str(g.ranges)' –