[R] Whats wrong with NextMethod("[[<-")?

Henrik Bengtsson hb at maths.lth.se
Sat Oct 25 19:29:11 CEST 2003


> -----Original Message-----
> From: Prof Brian Ripley [mailto:ripley at stats.ox.ac.uk] 
> Sent: den 25 oktober 2003 18:01
> To: Henrik Bengtsson
> Cc: 'Eryk Wolski'; r-help at stat.math.ethz.ch
> Subject: RE: [R] Whats wrong with NextMethod("[[<-")?
> 
> 
> On Sat, 25 Oct 2003, Henrik Bengtsson wrote:
> 
> > Hi. NextMethod("[[<-") do indeed call the "next" "[[<-"() 
> method. The 
> > thing is that it is also return the update object, which 
> you are *not* 
> > taking care of. You are just returning the same object 
> again. So here 
> > is what you want:
> > 
> > "[[<-.myA"<-function(mvl,name,value) {
> >   if(data.class(value) == "myB") {
> >     mvl$data[[name]] <- value
> >     mvl
> >   } else {
> >     NextMethod("[[<-")
> >   }
> > }
> > 
> > Also, it is better to use data.class(obj) than class(obj)[1].
> 
> It's not.  First, the effect is the same here, and second 
> data.class is really only for back-compatibility when fudging the 
> difference between "numeric" and "integer".

I didn't really know that. Thanks for that comment.

> It is almost certainly better to use inherits(value, "myB"), 
> though, and you should probably very rarely test the class 
> (let alone data.class) directly.

Of course! ...and to extend what Brian Ripley write: you rarely want to
do an action *if and only if* an object is of a specific class and
another action if it is of a subclass or a superclass. This would in
some would be against why you have choose the class hierarchy that
chose. So inherits() should be used.

Henrik "too-little-sleep" Bengtsson

> > Cheers
> > 
> > Henrik Bengtsson
> > Lund University
> > 
> > 
> > > -----Original Message-----
> > > From: r-help-bounces at stat.math.ethz.ch
> > > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Eryk Wolski
> > > Sent: den 25 oktober 2003 16:27
> > > To: r-help at stat.math.ethz.ch
> > > Subject: [R] Whats wrong with NextMethod("[[<-")?
> > > 
> > > 
> > > Hi!
> > > 
> > > Given a list object "myA" with several fields
> > > i want to construct the assignment function "[[<-.myA" 
> which behaves 
> > > differently dependent of the class of the arguments (value).
> > > 
> > > If value are a myB object it should append(assign) this
> > > massvector to the data field of myA object if its any other 
> > > object the assignement function should work as for any list.
> > > 
> > > 
> > > "[[<-.myA"<-function(mvl,name,value)
> > >   {
> > >     if(class(value)[1]!="myB")
> > >       {
> > >         NextMethod("[[<-"); #work as a list
> > >       }
> > >     else
> > >       {
> > >         mvl$data[[name]] <- value #work as myA
> > >       }
> > >     mvl
> > >   }
> > > 
> > > 
> > > test<-list(a=1,data=list(1))
> > > class(test)<-c("myA","list")
> > > 
> > > duda<-"hello"
> > > class(duda)<-"myB"
> > > 
> > > test[[2]]<-duda               #works
> > > test[["newField"]]<-"bella"   #nothing happens!!!
> > > 
> > > The assignment test[["newField"]]<-"bella" has no effect. The
> > > list test are not extended by the entry newField. Looks to me 
> > > like NextMethod("[[<-") does nothing.
> > > 
> > > Is it possible to do this what I try to do with S3?
> > > If so what i do wrong?
> > > If its not possible to do this in this way can anyone suggest  a 
> > > different solution to what i like to do?
> > > 
> > > Eryk
> > > 
> > > ______________________________________________
> > > R-help at stat.math.ethz.ch mailing list
> > > https://www.stat.math.ethz.ch/mailma> n/listinfo/r-help
> > > 
> > >
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list 
> > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
> > 
> > 
> 
> -- 
> Brian D. Ripley,                  ripley at stats.ox.ac.uk
> Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
> University of Oxford,             Tel:  +44 1865 272861 (self)
> 1 South Parks Road,                     +44 1865 272866 (PA)
> Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list