Skip to content
Prev 327297 / 398502 Next

Check the class of an object

Hi,
On 07/23/2013 09:59 AM, Simon Zehnder wrote:
Unlike with S3 objects, class() on an S4 object can only return 1 class.

Also note that, on an S3 object, doing

   "firstClass" %in% class(myObject)

is equivalent to doing inherits(myObject, "firstClass"), which is
what you said you wanted to avoid. The most specific class should be
the first so if that's what you wanted to check, you could do

   class(myObject)[1] == "firstClass"

But that precaution is not needed if 'myObject' is guaranteed to be
an S4 object (although when writing a unit test, one should probably
discard any guarantee of that sort).

Cheers,
H.