[Bioc-devel] adding a validity method to a class

Paul Shannon pshannon at systemsbiology.org
Mon Apr 4 19:38:14 CEST 2011


Combining suggestions from Hervé and Steve Lianoglou, my confusion is cleared up.  The custom validity function has to be defined before it is named in the class definition.

This works:

   myValidity = function (object) cat ('hello from myValidity')
   setClass ("A", representation(blob="raw") , validity=myValidity)
   a <- new ("A")
   print (validObject(a))

I expected the reference to the method to be resolved dynamically when validObject was first called.  Not so.

Thanks to everyone who helped on this.

 - Paul



On Mar 31, 2011, at 12:08 PM, Hervé Pagès wrote:

> Hi Paul,
> 
> On 03/31/2011 10:42 AM, Paul Shannon wrote:
>> I have defined a rather lengthy validity function for CytoscapeWindowClass.
>> 
>> In my testing, I create incomplete or broken instances of the class.
>> 
>> I get the results I want when I call the validity function like this, for example:
>> 
>>   >  RCytoscape:::validCytoscapeWindow (cw.broken)
>>    You must provide an 'edgeType' edge attribute, which will be mapped to Cytoscape's 'interaction' edge attribute.
>>    [1] FALSE
>> 
>> But when, following the example in the graphNEL source code, I try to assign this function to the 'validity' slot of the class, the function is not found.
>> 
>> The class definition:
>> 
>>   setClass ("CytoscapeWindowClass",
>>             representation = representation (title="character",
>>                                              window.id='character',
>>                                              graph="graph"),
>>             contains='CytoscapeConnectionClass',
>>             prototype = prototype (title="R graph",
>>                                   graph=new ("graphNEL", edgemode='directed'),
>>                                    uri="http://localhost:9000"),
>>             validity=function(obj) validCytoscapeWindow (obj)
>>             #validity=function(obj) RCytoscape:::validCytoscapeWindow (obj)   # I also tried this
>>             )
>> 
>> Calling 'validObject (cw)' should invoke 'validCytoscapeWindow' but does not.
> 
>                        ^^
> Are you calling validObject() on 'cw' or on 'cw.broken'?
> 
> It should work:
> 
>  setClass("A", representation(blob="raw"),
>                validity=function(object) cat("Hi!\n"))
> 
> Then:
> 
>  > a <- new("A")
>  > validObject(a)
>  Hi!
>  [1] TRUE
> 
> Cheers,
> H.
> 
>> 
>> Can anyone explain what I am doing wrong?
>> 
>> Thanks!
>> 
>>  - Paul
>> 
>> _______________________________________________
>> Bioc-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/bioc-devel
> 
> 
> -- 
> < Pagès
> 
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M2-B876
> P.O. Box 19024
> Seattle, WA 98109-1024
> 
> E-mail: hpages at fhcrc.org
> Phone:  (206) 667-5791
> Fax:    (206) 667-1319



More information about the Bioc-devel mailing list