Re: [R] Question about setReplaceMethod

From: Martin Morgan <mtmorgan_at_fhcrc.org>
Date: Thu, 24 May 2007 20:41:21 -0700

adschai_at_optonline.net writes:

> Sorry Martin. I had that line in my actual code. It did not work. I
> looked around all the google but it doesn't seem to have answer to
> this. Any ideas? Thank you.

A more complete solution -- remember to return 'this' throughout (R is 'pass by value' rather than pass by reference, so changes inside functions are only changes to the _local_ definition). Some of this is only guessing at your intention...

setClass("Foo",

         representation(x="data.frame", y="character")) setGeneric("setX<-",

           function(this, value) standardGeneric("setX<-")) setReplaceMethod("setX",

                 signature=signature("Foo", "data.frame"),
                 function(this,value) {
                     this_at_x <- value
                     this
                 })

setGeneric("generateFrame",
           function(this) standardGeneric("generateFrame"))
           

## generateFrame seems like it is not meant to be a replacement method setMethod("generateFrame",

          signature=signature(this="Foo"),
          function(this) {
              frame <- data.frame(x=letters[1:5], y=letters[5:1])
              setX(this) <- frame # modifies this_at_x
              this             # return 'this', an object of class Foo
          })

foo <- function() {

    objFoo <- new("Foo", x=data.frame(NULL), y="")     cat("objFoo (after new)\n")
    print(objFoo)
    ## now assign 'frame' the results of generateFrame, i.e., an     ## object of class 'Foo'. objFoo does not change     frame <- generateFrame(objFoo)
    cat("frame:\n")
    print(frame);
    ## change the value of objFoo_at_x
    setX(objFoo) <- data.frame(x=LETTERS[1:5], y=LETTERS[5:1])     cat("objFoo (after setX):\n")
    print(objFoo)
    ## what to return?? maybe just 'ok', and lose all our changes!     "ok"
}

>

>  
>

> - adschai
> ----- Original Message -----
> From: Martin Morgan @FHCRC.ORG>
> Date: Thursday, May 24, 2007 9:35 pm
> Subject: Re: [R] Question about setReplaceMethod
> To: adschai_at_optonline.net
> Cc: r-help_at_stat.math.ethz.ch
>> Hi Adschai --
>>
>> You'll want to return the value whose slot you have modified:
>>
>> setReplaceMethod("setX", "foo",
>> function(this,value) {
>> this_at_x <- value
>> this # add this line
>> })
>>
>> Martin
>>
>> adschai_at_optonline.net writes:
>>
>> > Hi
>> >
>> > I have the code like I show below. The problem here is that I
>> have a
>> > setReplacementMethod to set the value of my class slot. However,
>> > this function doesn't work when I call it within another function
>> > definition (declared by setMethod) of the same class. I do not
>> > understand this behavior that much. I'm wondering how to make this
>> > work? Any help would be really appreciated. Thank you.
>> >
>> > setClass("foo",
>> > representation(x="data.frame", y="character"))
>> > setGeneric("setX<-", function(this, value),
>> standardGeneric("setX<-"))
>> > setReplaceMethod("setX", "foo",
>> > function(this,value) {
>> > this_at_x <- value
>> > })
>> > setGeneric("generateFrame", function(this),
>> standardGeneric("generateFrame"))>
>> setReplaceMethod("generateFrame", "foo",
>> > function(this) {
>> > frame <- read.csv(file="myfile.csv", header=T) # read some
>> input file
>> > this_at_x <- frame # this doesn't replace the value for me
>> > setX(this) <- frame # this doesn't replace the value for me
>> > frame # instead I have to return the frame object
>> > })
>> > foo <- function(x,y) {
>> > objFoo <- new("foo", x=data.frame(NULL), y="")
>> > frame <- generateFrame(objFoo) # after this point, nothing got
>> assigned to objFoo_at_x
>> > setX(objFoo) <- frame # this will work (why do I have to
>> duplicate this??)
>> > }
>> > - adschai
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help_at_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
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>> --
>> Martin Morgan
>> Bioconductor / Computational Biology
>> http://bioconductor.org
>>

-- 
Martin Morgan
Bioconductor / Computational Biology
http://bioconductor.org

______________________________________________
R-help_at_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
and provide commented, minimal, self-contained, reproducible code.
Received on Fri 25 May 2007 - 03:48:42 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 25 May 2007 - 06:31:35 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.