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

Ross Boylan ross at biostat.ucsf.edu
Wed Jun 8 02:44:18 CEST 2005


On Tue, 2005-06-07 at 13:49 -0700, Berton Gunter wrote:
> > 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")
> > 
With some help from Bert, partly offlist, here's a working version:
setReplaceMethod("bumpIndex",
                 signature=signature(pm="CompletePathMaker",
                   value="numeric"), function(pm, value) {
                     pm at i <- pm at i+as.integer(value)
                     pm
                   })
At least 2 problems were caused by my original, final argument of
bumpIndex to setReplaceMethod:
1) This looked for the function bumpIndex, not bumpIndex<-.  That's why
I had to define the bumpIndex function.  With the above change, it is no
longer necessary to define bumpIndex.  I needed to point it to
bumpIndex<-.  I've been unable to find how to quote that properly.

2) The bumpIndex function doesn't have the right arguments.

By the way, the use of "value" as the name for the final argument to the
assignment function is mandatory.

The info about value, as well as an extensive discussion of issues with
mutating objects, appear in this 2003 tutorial by Gentleman:
http://www.stat.auckland.ac.nz/S-Workshop/Gentleman/S4Objects.pdf
(thanks to Bert for the pointer).

Ross




More information about the R-help mailing list