我想添加一个条件来测试当前变量是否是两个类中任意一个的对象:
test if db present any of 2 classes
示例1
if (!inherits(db, c("bioData", "character"))) {
cat("\n\t Error:\n")
cat("\t database must be bioData or character class \n")
stop()
}
示例 2,可行,但有效吗?
if (!class(db)[1] %in% c("bioData", "character")) {
cat("\n\t Error:\n")
cat("\t database must be bioData or character class \n")
stop()
}
您可以将
inherits
放入 mapply
中,您所需要的只是您喜欢的类别的向量。
> if (!any(mapply(inherits, list(X), c('foo', 'bar', 'baz')))) stop('wrong class')
> if (!any(mapply(inherits, list(Y), c('foo', 'bar', 'baz')))) stop('wrong class')
Error: wrong class
数据:
> X <- structure(0, class=c(class(0), c('foo', 'bar', 'baz')))
> Y <- structure(0, class=c(class(0), c('bam', 'bat')))