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

From: Henrik Bengtsson <hb_at_maths.lth.se>
Date: Wed 08 Jun 2005 - 07:54:14 EST

Berton Gunter wrote:
>>Second, in my experiments I couldn't get setReplacementMethod to work:
>>
>>"bumpIndex<-" <- function(pm, value) {
>> pm@i <- pm@i+as.integer(value)
>> pm
>>}
>>
>># I get an error without the next function definition
>>bumpIndex <- function(pm) pm@i
>>
>>setReplaceMethod("bumpIndex",
>> signature=signature(pm="CompletePathMaker",
>> value="numeric"), bumpIndex)
>>

> 
> 
> 
> 'bumpIndex' is apparently not a generic function (created by setGeneric)
> signature is not an argument to setReplaceMethod 
> The ... argument to setReplaceMethod must include a function to do the
> replacement, which is why you need the bumpIndex function (I agree that this
> is not cleanly documented, but it's fairly easy to see since
> setReplaceMethod calls setMethod and that requires the function argument).
> 
> so I don't see why you expect this to work at all. At the very least,
> shouldn't you do what the documentation tells you to? I think that you are
> expecting R to work according to your paradigm rather than trying to
> understand how it actually works. If you feel that R's paradigm (and/or
> documentation) is too awful to bother with, you might consider the R.oo
> package, which does OOP an entirely different way.

I have not followed the thread before, but here is an how it could look like with R.oo:

setConstructorS3("CompletePathMaker", function(index=0) {

   extend(Object(), "CompletePathMaker",      index = as.integer(index)
   )
})

setMethodS3("increase", "CompletePathMaker", function(this, dindex=+1) {

   this$index <- this$index + as.integer(dindex);    # You do not have to return anything here.... })

cpm <- CompletePathMaker()
cpm$index <- 3
increase(cpm, 2)
print(cpm$index)

To add some control of what type of values you can assign to 'index', you can define a virtual field function set<Field>(), e.g.

setMethodS3("setIndex", "CompletePathMaker", function(this, index) {

   if (!is.numeric(index) || length(index) != 1)      stop("Argument 'index' must be a single numeric: ", mode(index));    if (index < 0)
     stop("Argument 'index' must be non-negative: ", index);    this$index <- as.integer(index);
   # ... or here.
})

cpm$index <- 4
cpm$index <- -3 # Gives an error as wanted!

(Again...) note that setConstructorS3() and setMethodS3() are conveniency wrappers to define the CompletePathMaker(), increaseBumpIndex.CompletePathMaker() and setBumpIndex.CompletePathMaker(). The implementation of references is taken care of by the special class Object, which also implements support for virtual fields.

/Henrik

> -- Bert Gunter
> 
> 
> 
> 

>>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@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
>>
>>
> 
> 
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
> 
>

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Wed Jun 08 08:03:33 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:32:27 EST