[R] setMethod("min", "myclass", ...)

Peter Ruckdeschel Peter.Ruckdeschel at uni-bayreuth.de
Wed Dec 10 14:18:27 CET 2003


Hello,

first of all thank you for your reply to our help request.

On Wed, 3 Dec 2003, John Chambers wrote:

>Thomas Stabla wrote:
>  
>
>>I have defined a new class
>>    
>>
>>>setClass("myclass", representation(min = "numeric", max = "numeric"))
>>>      
>>>
>>and want to write accessor functions, so that for
>>    
>>
>>>foo = new("myclass", min = 0, max = 1)
>>>min(foo) # prints 0
>>>max(foo) # prints 1
>>>      
>>>
>>At first i created a generic function for "min"
>>    
>>
>>>setGeneric("min", function(..., na.rm = FALSE) standardGeneric("min"))
>>>      
>>>
>>and then tried to use setMethod. And there's my problem, I don't know the
>>name of the first argument which is to be passed to "min".
>>I can't just write:
>>    
>>
>>>setMethod("min", "myclass", function(..., na.rm = FALSE) ... at min)
>>>      
>>>
>>The same problem occurs with "max".
>>    
>>
>
>Generally, it's not a good idea to take a well-known function name and
>make it into an accessor function.
>  
>

We do agree with you in that point in general.

Perhaps we should be a little more precise with our problem:
We are working on a distribution class which is to provide an object
orientated way to allow for (semi--)automatical generation of new
distributions out of existing ones

that is, we want to allow for expressions like
  X+Y        for X~F ,  Y~G,  F,G  implementated distributions
and then to automatically generate the [r,d,p,q]-functions for X+Y

another aim will be to allow to pass distributions as
arguments to other functions etc...

In our case, the function   (min)   already  takes the role of a 
parameter to  *base* functions, i.e. runif, dunif, punif, qunif, 
in the sequel denoted by [r,d,p,q]unif;

the same argument applies to the functions   (mean)    and   (sd)   
which are parameters to [r,d,p,q]norm.

As we want to build distribution classes with a parameter slot using
these functions, it seemed natural to us to use the argument names of
these existing functions.
Perhaps a simple compromise (which we would tend to use now) is to use 
capitalized versions of the names for our slots.

>In a functional language, basic function calls such as min(x), sin(x),
>etc. should have a natural interpretation.  Defining methods for these
>functions is meant to do what it says:  provide a method to achieve the
>general purpose of the function for particular objects.
>  
>
We agree with you again, and in the particular case of our distribution

(a) min, max applied to a distribution should well be interpreted as min
and max of the support of this distribution

(b) mean and sd for a normal distribution also have their natural meaning

So in both cases, these functions do have a natural interpretation and 
do provide a method to achieve the general purpose of the function to 
our object.

>If you want accessor functions, they should probably have names that
>make their purpose obvious.  One convention, a la Java properties, would
>be getMin(x), etc. (the capitalizing is potentially an issue since slot
>names are case sensitive).
>
We do not like this so much, as we would prefer keeping as close as
possible to the naming convention of the parameters already defined
in the [r,d,p,q]-<distribution> functions in the base package.
So probably capitalizing is the thing we should do.

> .... [Rest of reply snipped]...

Thank you once again for your attention,

-- 
Peter Ruckdeschel 
Thomas Stabla
Matthias Kohl
Florian Camphausen




More information about the R-help mailing list