[R] Changing the value of an object's slot in a method

tofesi@web.de tofesi at web.de
Fri Sep 3 15:27:48 CEST 2004

Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Received-SPF: none (hypatia: domain of tofesi at web.de does not designate permitted sender hosts)
X-Virus-Scanned: by amavisd-new at stat.math.ethz.ch
X-Spam-Checker-Version: SpamAssassin 2.63 (2004-01-11) on hypatia.math.ethz.ch
X-Spam-Status: No, hits=0.2 required=5.0 tests=BAYES_50,NO_REAL_NAME autolearn=no version=2.63

Hi all,

I want to modify the value of an object's slot from within a method, as in the example below:

setClass("MyClass", representation(flag="numeric"))
if( !isGeneric("flipFlag") )
    setGeneric("flipFlag", function(object)
          function(object) {

            # flips object at flag from 0 to 1, or from 1 to 0

This should work in a similar way as in other object-oriented languages where it would be written as 'object.flipFlag()'. However, there seems to be a problem with the call-by-value semantics of R, and the updated value is lost on leaving the function.

The following two options are not suitable for our task:
1.) Directly assigning to 'flag' via a method created by 'setReplaceMethod'. The method 'flipFlag' should require no parameters.
2.) Returning a new object, so that the user has to assign the new object to the old variable.

Thanks in advance,
Verschicken Sie romantische, coole und witzige Bilder per SMS!

More information about the R-help mailing list