[Rd] How to force dispatch to an internal generic?

Skye Bender-deMoll skyebend at skyeome.net
Fri Dec 20 19:37:11 CET 2013


Dear R-devel,

I have a class 'myClass' in R that is essentially a list with 
pre-specified structure. It has an assignment operator which is going to 
do some things and then should assign the value using the regular list 
assignment operator

`$<-.myClass`<-function(x,i,value){
    # do some pre-processing  stuff

    # make the assignment using the default list assignment
    x[[i]]<-value
    x
  }

But I can't actually use x[[i]]<-value as it will dispatch to the 
already existing [[<-.myClass method.

In similar S3 dispatching cases, I've been able use UseMethod or 
specifically call [[<-.list, or [[<-.default but those don't seem to 
exist because $<- and [[<- are primitive generics, right?  I couldn't 
figure out a way to effectively call call NextMethod, and I assume 
calling .Primitive("$<-") is not appropriate.

My current solution mirrors $<-.data.frame

`$<-.data.frame` <- function (x, name, value) {
   cl <- oldClass(x)
   class(x) <- NULL
   x[[name]] <- value
   class(x) <- cl
   x
}

but according to tracemem(), this is triggering three deep copies of 
myClass objects (which can be expensive as the object is often very 
large ) with each $ assignment instead of one.

Is there a better way to dispatch the assignment to the default 
assignment method?


Note: I previously posted the same question at
http://stackoverflow.com/questions/20627776/how-to-force-dispatch-to-an-internal-generic-in-r

thanks for your help,
  -skye



More information about the R-devel mailing list