[R] How to change the value of a class slot

Ross Boylan ross at biostat.ucsf.edu
Tue Jun 7 20:36:17 CEST 2005


On Mon, 2005-06-06 at 14:15 -0700, Berton Gunter wrote:
> I'm puzzled:
> 
> > It looks as if instances of class objects are best thought of as
> > immutable once created.
> > 
> 
> what then is setReplaceMethod() for? 
assignment operators do the whole object replacement behind the scenes,
at least conceptually, as far as I can tell.  I agree: they are
mutators.  But outside of this special case, it seems mutation of slots
is difficult (i.e., requires the assistance of the caller).

By the way, the documentation on setReplaceMethod does not actually say
what it does.  I found out by looking at the code.

Second, in my experiments I couldn't get setReplacementMethod to work:

"bumpIndex<-" <- function(pm, value) {
  pm at i <- pm at i+as.integer(value)
  pm
}

# I get an error without the next function definition
bumpIndex <- function(pm) pm at i

setReplaceMethod("bumpIndex",
                 signature=signature(pm="CompletePathMaker",
                   value="numeric"), bumpIndex) 

When I try to load this, I get
arguments in definition changed from (spec) to (object)
arguments in definition changed from (self) to (object)
arguments in definition changed from (self) to (object)
Creating a new generic function for 'bumpIndex<-' in '.GlobalEnv'
Error in conformMethod(signature, mnames, fnames, f) : 
	In method for function "bumpIndex<-": formal arguments omitted in the
method definition cannot be in the signature (value = "numeric")

All the errors are triggered by setReplaceMethod.  Can anyone help me
interpret them?  Or, maybe better, tell me how to debug the
"compilation"?


> I leave it to language "experts" to say whether S4 formal classes and
> methods are wise or not in comparison to others. From my fairly ignorant
> perspective, that always seems to be a matter of taste.

There are actually two related issues on that score: first, whether the
complex of expectation set up by talking about "objects" and "classes"
are met by what R/S does, and second the wisdom of what R/S does in its
own right.

> 
> Cheers,
> Bert
> 
> 
-- 
Ross Boylan                                      wk:  (415) 502-4031
530 Parnassus Avenue (Library) rm 115-4          ross at biostat.ucsf.edu
Dept of Epidemiology and Biostatistics           fax: (415) 476-9856
University of California, San Francisco
San Francisco, CA 94143-0840                     hm:  (415) 550-1062




More information about the R-help mailing list