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

Eryk Wolski wolski at molgen.mpg.de
Sat Oct 25 16:26:36 CEST 2003


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




More information about the R-help mailing list