For information on using this system, please visit this page.

Bug 17141 - New class containing factor does not inherit method for factor
Summary: New class containing factor does not inherit method for factor
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: S4methods (show other bugs)
Version: R 3.3.*
Hardware: x86_64/x64/amd64 (64-bit) Windows 64-bit
: P5 enhancement
Assignee: R-core
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-09-04 14:13 UTC by RM
Modified: 2016-09-19 12:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description RM 2016-09-04 14:13:35 UTC
On the  R version 3.3.1 when making a new class that contains a factor as the S3 part of the class it looks like the new class does not inherit behavior expected from a class that is based on a factor type. 
> setClass('newFactor', representation = c(tempValue='character'), contains =
> 'factor')
> ttt<-new('newFactor',tempValue='ddd')
> S3Part(ttt)<-factor(c('a','b','c'))
> table(S3Part(ttt))

a b c 
1 1 1 
> table(ttt)
Error in as.character.factor(x) : attempting to coerce non-factor
>
Comment 1 RM 2016-09-04 14:41:51 UTC
BTW: the same code run with no errors on older version of R (3.2.?) and produce the expected results.
Comment 2 Michael Lawrence 2016-09-04 19:38:47 UTC
Could fix this by having the check use inherits3(), but the broader issue is that C-level inherits() is basically broken for S4 objects and has other inconsistencies with R-level inherits(). It's used all over the place. It could be made smarter, but since it's in Rinlinedfuncs.h, it does not have access to e.g. R_data_class(). Does it really have to be inlined?
Comment 3 RM 2016-09-05 20:18:02 UTC
(In reply to Michael Lawrence from comment #2)
> Could fix this by having the check use inherits3(), but the broader issue is
> that C-level inherits() is basically broken for S4 objects and has other
> inconsistencies with R-level inherits(). It's used all over the place. It
> could be made smarter, but since it's in Rinlinedfuncs.h, it does not have
> access to e.g. R_data_class(). Does it really have to be inlined?

I have ~10K lines of code with a very complex system so practically this change in the behavior of R not trivial to fix. 

The broader picture is that it breaks the entire concept of Object Oriented Programing (OOP). IfS4 class in R can contains (inherit) S3 object, for example: 
setClass('newFactor', representation = c(tempValue='character'), contains = 'factor')

then the new class should contain the functionality of the inherited class 'factor', otherwise S4 classes should not be able to inherit (contain) S3 classes.

I'm just trying to figure out my next steps and understand if/how this inconstancy with OOP consent will be resolved.
Comment 4 Michael Lawrence 2016-09-05 22:16:20 UTC
Just to clarify, that question was directed at others (in R core). This has exposed a fairly deep issue in R that should be discussed.
Comment 5 RM 2016-09-06 08:52:51 UTC
@Michael, 

Appreciate if you can provide a link to these discussions in R core, it will probably may be of interest for many users. 

Is there a way for me to join this group for further discussions?


 (In reply to Michael Lawrence from comment #4)
> Just to clarify, that question was directed at others (in R core). This has
> exposed a fairly deep issue in R that should be discussed.
Comment 6 Michael Lawrence 2016-09-06 13:36:48 UTC
Ideally that discussion would happen here, but it hasn't happened yet.
Comment 7 Michael Lawrence 2016-09-19 12:53:29 UTC
I've fixed this in devel for now by introducing C-level inherits2() that respects S4 inheritance (and implicit classes).