Hi the list, I define a class 'C' that inherit from two classes 'A' and 'B'. 'A' and 'B' have no slot with similar names. ---------------- setClass( Class="C", contains=c("A","B") ) To define the get operator '[' for class "C", I simply use the get of "A" or "B" (the constante 'SLOT_OF_A' is a character holding the names of all the slot of A) : ---------------- setMethod("[","C", function(x,i,j,drop){ if(i%in%SLOT_OF_A){ x <- as(x,'A') }else{ x <- as(x,'B') } return(x[i,j]) } ---------------- Is it possible to do something similar for the set operator '[<-' ? Thanks Christophe -- View this message in context: http://r.789695.n4.nabble.com/S4-defining-using-inheritance-from-2-classes-tp4082217p4082217.html Sent from the R help mailing list archive at Nabble.com.
Martin Morgan
2011-Nov-18 22:29 UTC
[R] S4 : defining [<- using inheritance from 2 classes
On 11/17/2011 09:46 PM, cgenolin wrote:> Hi the list, > > I define a class 'C' that inherit from two classes 'A' and 'B'. 'A' and 'B' > have no slot with similar names. > ---------------- > setClass( > Class="C", > contains=c("A","B") > ) > > > To define the get operator '[' for class "C", I simply use the get of "A" or > "B" (the constante 'SLOT_OF_A' is a character holding the names of all the > slot of A) : > ---------------- > setMethod("[","C", > function(x,i,j,drop){ > if(i%in%SLOT_OF_A){ > x<- as(x,'A') > }else{ > x<- as(x,'B') > } > return(x[i,j]) > } > ---------------- > > Is it possible to do something similar for the set operator '[<-' ?Hi Christophe, can you provide a working example? The above seems highly unusual; normally one expects class(x[i, j]) = class(x). Best guess at what you're wrote above is setMethod("[", c("C", "character", "numeric"), function(x, i, j, ..., drop=TRUE) # conform to getGeneric("[") { if (!i %in% slotNames(x)) stop("cannot subset with '", i, "'") slot(x, i) = slot(x, i)[j] x }) which still seems quite unusual. Hope that helps, Martin> > Thanks > Christophe > > -- > View this message in context: http://r.789695.n4.nabble.com/S4-defining-using-inheritance-from-2-classes-tp4082217p4082217.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at 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: M1-B861 Telephone: 206 667-2793
Seemingly Similar Threads
- using setMethod or setGeneric to change S4 accessor symbol from @ to $
- Define S4 methods for 'plot'
- S4 inheritance and old class
- How to convert S4 class slots into data.frame or how to assign variables of type 'Date'
- S4 group "Math", "getGroupMembers", "genericForPrimitive"