Dear R-users,

I am working on a project that uses S4-classes. At some point I encountered the 
problem - well known to R - that I have to pass 3 different objects to a 
function, that should modify several slots of them and of course there is no 
passing by reference in R. 

Then I read this thread by Steve Lianoglou: 
https://stat.ethz.ch/pipermail/r-help/2010-August/250468.html, which offers 
from viewpoint of OOP an elegant solution. But I do not use an Object Method, 
but a regular function, pass in several objects and modify them.

Here is an example with 1 Object using Steve's approach: 

setClass("example", 
representation(
        par = "matrix",
        .cache = "environment")
)
setMethod("initialize", "example", function(.Object, ..., .cache = new.env()) {
                                                                
callNextMethod(.Object, .cache = .cache, ...)
                                                                }
)

ex <- new("example", par = matrix())

fmodify <- function(O1) {
        pm <- mean(rnorm(100))
        pm <- matrix(pm)
        O1@.cache$par <- pm
} 

fmodify(ex)


So far so good. Everything worked nicely. But of course the value computed in 
the function 'fmodify' is now in the slot ex@.cache$par:

ex@.cache$par

and not in ex@par. Is there a possibility to get it into ex@par?

Best

Simon

______________________________________________
R-help@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.

Reply via email to