rr-s4

What is the correct way to know which class an S4 object is?


if I have an S4 object, I can inquire if it's an S4 object with isS4()

> cls <- setClass("Foo", slots = c("hello"))
> f <- new("Foo")
> isS4(f)
TRUE

But it's not clear to me how I can find "Foo". class(f) seems to work, but it seems to me it's exploiting S3 mostly. Is there a correct way to find which class an S4 object belongs to?

Yes, I know about is(), but that assumes you already know the class name (e.g. is(f, "Foo"). I want to get "Foo" as a result.


Solution

  • class(f) is acceptable for both S3 and S4 objects. From the help page for class under "formal (S4) methods":

    "For objects which have a formal class, its name is returned by ‘class’ as a character vector of length one".