[R] Why 'validity' is not called? (S4)

Peng Yu pengyu.ut at gmail.com
Mon Oct 26 20:11:30 CET 2009


On Mon, Oct 26, 2009 at 1:49 PM, Martin Morgan <mtmorgan at fhcrc.org> wrote:
> Peng Yu wrote:
>> I thought that 'validity' defined in 'setClass' should be called in
>> 'new'. Could somebody let me know why 'validity' is not called? How to
>> make it be called?
>>
>>> setClass(
>> +     Class='A',
>> +     representation=representation(
>> +         x='numeric'
>> +         ),
>> +     validity=function(object){
>> +       cat("~~~ A: inspector ~~~\n")
>> +       if(object at x<0){
>> +         stop("[A:validation] object at x<0")
>> +       }
>> +       return(T)
>> +     }
>> +     )
>> [1] "A"
>>> setMethod(
>> +     f='initialize',
>> +     signature='A',
>> +     definition=function(.Object,x){
>> +       cat("~~~ A: initializator ~~~\n")
>> +       .Object at x=x
>> +       return(.Object)
>> +     }
>> +     )
>> [1] "initialize"
>
> The default initialize method (initialize,ANY-method) performs simple
> slot assignment and then calls validObject. You do not call the default
> method, so do not get slot assignment or validity checking. If you want
> only to initialize slots as above, then do not write any initialize
> method; let initialize,ANY-method do the work for you. Otherwise, use a
> paradigm like
>
>   .Object <- callNextMethod(.Object, x=x, ...)
>
> in your initialize method, so that the slot x is assigned and validity
> checked by the 'next' (eventually, initialize,ANY) method. Note that
> validity is checked in callNextMethod, so that the object has to be
> 'valid' after x has been assigned to it's slot.

I'm not clear what you mean by 'initialize,ANY-method'. Would you
please give an example based my test case in the original post?

> It is also possible to call validObject explicitly as part of your own
> initialize method.
>
> Two other issues. Calling new("A") with no arguments does NOT call
> validObject, so the default object (e.g., from the prototype argument)
> must be valid a priori. It may be useful to define a constructor A <-
> function(...) new("A", ...) both to provide a nicer interface to the
> user and to map between arguments the user might find convenient and
> slots the class wants to store.

I also don't quite understand these two issues. Would you please give
me some examples to help me understand them? Thank you!

> Martin
>
>>> new(Class='A', x=10)
>> ~~~ A: initializator ~~~
>> An object of class \u201cA\u201d
>> Slot "x":
>> [1] 10
>>
>> ______________________________________________
>> 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 M1 B861
> Phone: (206) 667-2793
>




More information about the R-help mailing list