Howdy all, I have a problem that I'd like some advice/help in solving---it has to do with R's pass-by-value system. I understand the issue, but am wondering if anyone has found a working solution in dealing with it for cases when one wants to modify an object inside of a method, specifically when working with S4. I'm aware that R.oo is able to deal with this using S3, but I'd really rather stick to S4. The basics of what I would like to do are coded below: setClass("MyMatrix", representation( parameters="matrix", uniqueCount="numeric" ), prototype( parameters=matrix(numeric(0),0,0), uniqueCount=1 ) ) setGeneric("createUniqueName", function(object) standardGeneric("createUniqueName")) setMethod("createUniqueName", "MyMatrix", function(object){ retval <- paste("unique_", object at uniqueCount, sep="") object at uniqueCount <- object at uniqueCount + 1 return(retval) }) x <- new("MyMatrix", parameters=matrix(0, 2, 2)) createUniqueName(x) # returns "unique_1" x # x at uniqueCount is still 1 I understand why this is happening, but am wondering how people in the community have dealt with it, specifically when using S4. Any advice would be appreciated. Also, I am aware that this is somewhat of a silly example, but it should allow you to see what I'm trying to accomplish. Thank you, Jeff. -- View this message in context: http://www.nabble.com/S4-pass-by-value-work-around--tp17997553p17997553.html Sent from the R help mailing list archive at Nabble.com.
Hi Jeff -- two different scenarios are to overwrite the current object, along the lines of y <- uniquify(y) where uniquify is a method like createUniqueName but returns the (modified) instance rather than unique name setMethod('uniquify', 'MyMatrix', function(x) { x at uniqueCount <- # something unique x }) The second is a replacement method, along the lines of setGeneric("uniqueCount<-", function(x, ..., value) standardGeneric("uniqueCount<-")) setReplaceMethod("uniqueCount", signature=c(x="MyMatrix", value="numeric"), function(x, ..., value) { x at uniqueCount <- value x }) uniqueCount(x) <- uniqueCount(x) + 1 x # now modified This is untested psuedo-code, so I hope it's right enough to get you going. Martin Jeffrey Spies <jspies2008 at gmail.com> writes:> Howdy all, > > I have a problem that I'd like some advice/help in solving---it has to do > with R's pass-by-value system. I understand the issue, but am wondering if > anyone has found a working solution in dealing with it for cases when one > wants to modify an object inside of a method, specifically when working with > S4. I'm aware that R.oo is able to deal with this using S3, but I'd really > rather stick to S4. > > The basics of what I would like to do are coded below: > > setClass("MyMatrix", > representation( > parameters="matrix", > uniqueCount="numeric" > ), > prototype( > parameters=matrix(numeric(0),0,0), > uniqueCount=1 > ) > ) > > setGeneric("createUniqueName", function(object) > standardGeneric("createUniqueName")) > > setMethod("createUniqueName", "MyMatrix", function(object){ > retval <- paste("unique_", object at uniqueCount, sep="") > object at uniqueCount <- object at uniqueCount + 1 > return(retval) > }) > > x <- new("MyMatrix", parameters=matrix(0, 2, 2)) > createUniqueName(x) # returns "unique_1" > x # x at uniqueCount is still 1 > > I understand why this is happening, but am wondering how people in the > community have dealt with it, specifically when using S4. Any advice would > be appreciated. Also, I am aware that this is somewhat of a silly example, > but it should allow you to see what I'm trying to accomplish. > > Thank you, > > Jeff. > > > -- > View this message in context: http://www.nabble.com/S4-pass-by-value-work-around--tp17997553p17997553.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.-- Martin Morgan Computational Biology / Fred Hutchinson Cancer Research Center 1100 Fairview Ave. N. PO Box 19024 Seattle, WA 98109 Location: Arnold Building M2 B169 Phone: (206) 667-2793
Quick comment. Use an environment to hold your "fields" and then pass around the environment variable. Any modification to variables inside the environment done from anywhere (inside/outside functions) will be persistent. Then wrap this up in a class structure, overload, say, '$', '[[', '$<-', '[[<-' to get() and assign() to your object and you have the first seed of what is done in R.oo. Note that '$' are '$<-' indeed available for environments. However, it is not recommended to extend the environment class itself. Instead you should wrap the environment up in a list and work from that. Search the R mailing list archives for issues you would/will face if you inherit from environment. My $.02 Henrik (R.oo author) On Wed, Jun 18, 2008 at 10:26 PM, Jeffrey Spies <jspies2008 at gmail.com> wrote:> > Howdy all, > > I have a problem that I'd like some advice/help in solving---it has to do > with R's pass-by-value system. I understand the issue, but am wondering if > anyone has found a working solution in dealing with it for cases when one > wants to modify an object inside of a method, specifically when working with > S4. I'm aware that R.oo is able to deal with this using S3, but I'd really > rather stick to S4. > > The basics of what I would like to do are coded below: > > setClass("MyMatrix", > representation( > parameters="matrix", > uniqueCount="numeric" > ), > prototype( > parameters=matrix(numeric(0),0,0), > uniqueCount=1 > ) > ) > > setGeneric("createUniqueName", function(object) > standardGeneric("createUniqueName")) > > setMethod("createUniqueName", "MyMatrix", function(object){ > retval <- paste("unique_", object at uniqueCount, sep="") > object at uniqueCount <- object at uniqueCount + 1 > return(retval) > }) > > x <- new("MyMatrix", parameters=matrix(0, 2, 2)) > createUniqueName(x) # returns "unique_1" > x # x at uniqueCount is still 1 > > I understand why this is happening, but am wondering how people in the > community have dealt with it, specifically when using S4. Any advice would > be appreciated. Also, I am aware that this is somewhat of a silly example, > but it should allow you to see what I'm trying to accomplish. > > Thank you, > > Jeff. > > > -- > View this message in context: http://www.nabble.com/S4-pass-by-value-work-around--tp17997553p17997553.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >