[R] the unwanted persistence of refClass methods

Ben Tupper btupper at bigelow.org
Fri Feb 14 15:48:22 CET 2014


On Feb 13, 2014, at 2:16 PM, Ross Boylan <ross at biostat.ucsf.edu> wrote:

> Redefining methods of a refClass does not change the methods for existing instances.  I found this somewhat surprising, and also pretty inconvenient for debugging.
> The documentation for reference classes does say that their methods should be minimalist, which I suppose is partly because of this behavior.
> Is there a way around this behavior apart from passing the pieces of the old instance to the new constructor, or putting all computations in not class functions?

I am interested in learning how others deal with this issue, too.

I use the latter method during development.  I write simple wrapper methods around the functions I am working on.  Once development becomes stable, I often choose to incorporate the function in the method but not always.  It's not a great system but it allows me to tweak the function until I get it right without a lot of fussing.

To my simpleton way of thinking, it seems like changes to the Reference Class definition might be 'pushable' into a particular instance's environment.  I don't know how to make that happen in a user friendly way.

foo_f = function(x, a = 3) {a * x}

foo <- setRefClass("foo")
foo$methods(f = function(x, ...) {foo_f(x, ...)})

x <- foo$new()
# [1] 30
x$f(10, a = 2)
# [1] 20


>> foo <- setRefClass("foo", methods=list(f=function(x) 3*x))
>> a <- foo()
>> a$f(10)
> [1] 30
>> foo <- setRefClass("foo", method=list(f=function(x) 2*x))
>> a$f(10)
> [1] 30
>> b <- foo()
>> b$f(10)
> [1] 20
> Ross Boylan
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

Ben Tupper
Bigelow Laboratory for Ocean Sciences
60 Bigelow Drive, P.O. Box 380
East Boothbay, Maine 04544

More information about the R-help mailing list