[R] S4; Setter function is not chaning slot value as expected

Martin Morgan mtmorgan at fhcrc.org
Sun Nov 10 15:49:34 CET 2013


On 11/09/2013 11:31 PM, Hadley Wickham wrote:
> Modelling a mutable entity, i.e. an account, is really a perfect
> example of when to use reference classes.  You might find the examples
> on http://adv-r.had.co.nz/OO-essentials.html give you a better feel
> for the strengths and weaknesses of R's different OO systems.

Reference classes provide less memory copying and a more familiar programming 
paradigm but not necessarily fantastic performance, as illustrated here

http://stackoverflow.com/questions/18677696/stack-class-in-r-something-more-concise/18678440#18678440

and I think elsewhere on this or the R-devel list (sorry not to be able to 
provide a more precise recollection).

Martin


>
> Hadley
>
> On Sat, Nov 9, 2013 at 9:31 AM, daniel schnaider <dschnaider at gmail.com> wrote:
>> It is my first time programming with S4 and I can't get the setter fuction
>> to actually change the value of the slot created by the constructor.
>>
>> I guess it has to do with local copy, global copy, etc. of the variable -
>> but, I could't find anything relevant in documentation.
>>
>> Tried to copy examples from the internet, but they had the same problem.
>>
>> # The code
>>      setClass ("Account" ,
>>                 representation (
>>                 customer_id = "character",
>>                 transactions = "matrix")
>>      )
>>
>>
>>      Account <- function(id, t) {
>>              new("Account", customer_id = id, transactions = t)
>>              }
>>
>>
>>      setGeneric ("CustomerID<-", function(obj,
>> id){standardGeneric("CustomerID<-")})
>>      setReplaceMethod("CustomerID", "Account", function(obj, id){
>>          obj at customer_id <- id
>>          obj
>>          })
>>
>>          ac <- Account("12345", matrix(c(1,2,3,4,5,6), ncol=2))
>>          ac
>>          CustomerID <- "54321"
>>          ac
>>
>> #Output
>>          > ac
>>          An object of class "Account"
>>          Slot "customer_id":
>>          [1] "12345"
>>
>>          Slot "transactions":
>>               [,1] [,2]
>>          [1,]    1    4
>>          [2,]    2    5
>>          [3,]    3    6
>>
>> # CustomerID is value has changed to 54321, but as you can see it does't
>>          > CustomerID <- "54321"
>>          > ac
>>          An object of class "Account"
>>          Slot "customer_id":
>>          [1] "12345"
>>
>>          Slot "transactions":
>>               [,1] [,2]
>>          [1,]    1    4
>>          [2,]    2    5
>>          [3,]    3    6
>>
>>
>> Help!
>>
>>          [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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.
>
>
>


-- 
Computational Biology / Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N.
PO Box 19024 Seattle, WA 98109

Location: Arnold Building M1 B861
Phone: (206) 667-2793



More information about the R-help mailing list