Skip to content

S4; Setter function is not chaning slot value as expected

8 messages · Simon Zehnder, Hadley Wickham, Martin Morgan +1 more

#
If you want to set a slot you have to refer to it:

ac at CustomerID <- ?54321? 

or you use your setter:

ac <- CustomerID(ac, ?54321?)

What you did was creating a new symbol CustomerID referring to the String ?54321?
CustomerID <- ?54321?
CustomerID
[1] ?54321?

Best

Simon
On 09 Nov 2013, at 15:31, daniel schnaider <dschnaider at gmail.com> wrote:

            
#
On 11/09/2013 06:31 AM, daniel schnaider wrote:
Replacement methods (in R in general) require that the final argument (the 
replacement value) be named 'value', so

     setGeneric("CustomerID<-",
         function(x, ..., value) standardGeneric("CustomerID"))

     setReplaceMethod("CustomerID", c("Account", "character"),
         function(x, ...., value)
     {
         x at customer_id <- value
         x
     })

use this as

    CustomerID(ac) <- "54321"

  
    
#
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.

Hadley
On Sat, Nov 9, 2013 at 9:31 AM, daniel schnaider <dschnaider at gmail.com> wrote:

  
    
#
On 11/10/2013 03:54 AM, daniel schnaider wrote:
I do not know. It is a requirement of replacement methods in R in general, not 
just S4 methods. See section 3.4.4 of RShowDoc("R-lang").
Copying is tricky in R. It behaves as though a copy has been made of the entire 
object. Whether a copy is actually made, or just marked as necessary on 
subsequent modification, requires deep consideration of the code. This is the 
way R works, not just the way S4 classes work.

If instead of a single account you modelled 'Accounts', i.e., all accounts, then 
updating 1000 account id's would only make one copy, whereas if you model each 
account separately this would require 1000 copies.

Martin

  
    
#
On 11/09/2013 11:31 PM, Hadley Wickham wrote:
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