Skip to content
Back to formatted view

Raw Message

Message-ID: <3322A432-5855-4A70-ADC9-20AA39FB5B66@uni-bonn.de>
Date: 2013-07-23T16:59:15Z
From: Simon Zehnder
Subject: Check the class of an object
In-Reply-To: <21F05392-CB77-49C0-A35E-3989F8E99BFA@comcast.net>

Hi David,

thanks for the reply. You are right. Using the %in% is more stable and I gonna change my code.

When testing for a specific class using 'is' one has to start at the lowest heir and walk up the inheritance structure. Starting at the checks at the root will always give TRUE. Having a structure which is quite complicated let me move to the check I suggested in my first mail. 

Best

Simon

On Jul 23, 2013, at 6:15 PM, David Winsemius <dwinsemius at comcast.net> wrote:

> 
> On Jul 23, 2013, at 5:36 AM, Simon Zehnder wrote:
> 
>> Dear R-Users and R-Devels,
>> 
>> I have large project based on S4 classes. While writing my unit tests I found out, that 'is' cannot test for a specific class, as also inherited classes can be treated as their super classes. I need to do checks for specific classes. What I do right now is sth. like
>> 
>> if (class(myClass) == "firstClass") {
> 
> I would think that you would need to use `%in%` instead. 
> 
> if( "firstClass" %in% class(myObject) ){
> 
> Objects can have more than one class, so testing with "==" would fail in those instances.
> 
> 
>> 
>> } else if (class(myClass) == "secondClass") {
>> 
>> }
>> 
>> Is this the usual way how classes are checked in R?
> 
> Well, `inherits` IS the usual way.
> 
>> I was expecting some specific method (and 'inherits' or 'extends' is not what I look for)...
>> 
>> 
>> Best
>> 
>> Simon
>> 
>> 	[[alternative HTML version deleted]]
> 
> Plain-text format is the recommended format for Rhelp
> 
> -- 
> David Winsemius
> Alameda, CA, USA
>