0

We all know functions like is.data.frame or is.double etc. Probably easy to do but hard to google: How can create your own is.? function? Is there a better way to do it then:

is.myClass <- function(x){
if(class(x) %in% "myClass") return(TRUE)
else return(FALSE)
}
Matt Bannert
  • 27,631
  • 38
  • 141
  • 207
  • This question seems related: http://stackoverflow.com/questions/5158830/identify-all-objects-of-given-class-for-further-processing – Chase Nov 28 '12 at 16:23
  • I think that should go the other way around: `function(x) {"myClass" %in% class(x)}`, but as @James points out `inherits` is more idiomatic (I don't know whether it has any other advantages) – Ben Bolker Nov 28 '12 at 16:32

1 Answers1

4

Perhaps inherits is enough:

is.myClass <- function(x) {inherits(x,"myClass")}

x <- 1
is.myClass(x)
[1] FALSE
class(x) <- c(class(x),"myClass")
is.myClass(x)
[1] TRUE
James
  • 65,548
  • 14
  • 155
  • 193