[R] Check the class of an object
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) { } else if (class(myClass) == secondClass) { } Is this the usual way how classes are checked in R? I was expecting some specific method (and 'inherits' or 'extends' is not what I look for)... Best Simon [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
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 dwinsem...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
On 07/23/2013 09:59 AM, Simon Zehnder wrote: Hi David, thanks for the reply. You are right. Using the %in% is more stable and I gonna change my code. you said you were you were using S4 classes. S4 classes do not report vectors of length != 1, from ?class For objects which have a formal class, its name is returned by 'class' as a character vector of length one so a first unit test could be stopifnot(length(class(myObject)) != 1L) 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 dwinsem...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
Hi, On 07/23/2013 09:59 AM, Simon Zehnder wrote: Hi David, thanks for the reply. You are right. Using the %in% is more stable and I gonna change my code. 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. 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 dwinsem...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpa...@fhcrc.org Phone: (206) 667-5791 Fax:(206) 667-1319 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
Hi Martin, I didn't know that. But that is even more comfortable for checking. Thanks for the quick update! Best Simon P.S. And thanks for the online documents about S4 - I could already learn a lot! On Jul 23, 2013, at 7:11 PM, Martin Morgan mtmor...@fhcrc.org wrote: On 07/23/2013 09:59 AM, Simon Zehnder wrote: Hi David, thanks for the reply. You are right. Using the %in% is more stable and I gonna change my code. you said you were you were using S4 classes. S4 classes do not report vectors of length != 1, from ?class For objects which have a formal class, its name is returned by 'class' as a character vector of length one so a first unit test could be stopifnot(length(class(myObject)) != 1L) 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 dwinsem...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M1 B861 Phone: (206) 667-2793 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Check the class of an object
Hi Hervé, thank you very much for your reply! This makes the different treatment of S3 and S4 objects by 'class' clear. Best Simon On Jul 23, 2013, at 7:20 PM, Hervé Pagès hpa...@fhcrc.org wrote: Hi, On 07/23/2013 09:59 AM, Simon Zehnder wrote: Hi David, thanks for the reply. You are right. Using the %in% is more stable and I gonna change my code. 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. 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 dwinsem...@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 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Hervé Pagès Program in Computational Biology Division of Public Health Sciences Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N, M1-B514 P.O. Box 19024 Seattle, WA 98109-1024 E-mail: hpa...@fhcrc.org Phone: (206) 667-5791 Fax:(206) 667-1319 __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.