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

From: Ross Boylan <ross_at_biostat.ucsf.edu>
Date: Sat 04 Jun 2005 - 10:04:08 EST


I defined an S4 class with a slot i. Then I wrote a regular function that attempted to increment i.

This didn't work, apparently because of the general rule that a function can't change the values of its arguments outside the function. I gather there are ways around it, but the Green book admonishes "cheating on the S evaluation model is to be avoided" (p. 190).

Thinking that class methods needed to an exception to this rule, I then tried setMethod with the function I had written. However, when I called the function I got
> setMethod("nextPath", "CompletePathMaker", nextPath)
Creating a new generic function for 'nextPath' in '.GlobalEnv' [1] "nextPath"
> nextPath(pm)

Error: protect(): protection stack overflow

I can change the value of the slot interactively, so the problem does not appear to be that the slots are considered off-limits.

What do I need to do to update slot values?

Here are some possibly relevant code fragments setClass("CompletePathMaker",

         representation(i="integer",
                        timeOffset="numeric", # to avoid 0's
                        truePaths="TruePaths")
         )

nextPath <- function(pm){ #pm is a CompletePathMaker   pm@i <- pm@i+as.integer(1)
[etc]

I'm trying to make the class behave like an iterator, with i keeping track of its location. I'm sure there are more R'ish ways to go, but I'm also pretty sure I'm going to want to be able to update slots.

Thanks.
Ross Boylan



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 Sat Jun 04 10:12:26 2005

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