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

John Chambers jmc at research.bell-labs.com
Wed Dec 3 17:03:50 CET 2003


Thomas Stabla wrote:
> 
> Hello,
> 
> 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.

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.

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

If you do really want a method for the min() function for myclass,
that's a different problem.

The argument "..." is different from all other argument names, and it
can't be used in a signature.  To define methods for functions such as
min(), the formal arguments of the basic function would have to be
changed to, say, function(x, ..., na.rm)

Then methods can be defined for argument "x".

The R versions of these functions don't currently allow methods.  If
methods are needed, a package could currently define its own version of
the (non-generic) functions to include argument "x".

More than this is needed to handle multiple arguments generally.  The
problem is that defining a method for argument "x" does not cause that
method to be called if the object appears as a later argument.

If you had a method for min() for "myclass" and myX was an object from
that class
  min(myX, 1)
would work, but
  min(1, myX)
would not.  To get all examples right would require changes to the basic
code for these functions.  There is a brief discussion of one approach
in "Programming with Data", pages 343 and 351.






> 
> Thanks for your help.
> 
> Greetings,
> Thomas Stabla
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help

-- 
John M. Chambers                  jmc at bell-labs.com
Bell Labs, Lucent Technologies    office: (908)582-2681
700 Mountain Avenue, Room 2C-282  fax:    (908)582-3340
Murray Hill, NJ  07974            web: http://www.cs.bell-labs.com/~jmc




More information about the R-help mailing list