[R] checking generic/method consistency

Martin Maechler maechler at stat.math.ethz.ch
Tue Sep 30 15:23:10 CEST 2003


>>>>> "Arne" == Arne Henningsen <ahenningsen at email.uni-kiel.de>
>>>>>     on Tue, 30 Sep 2003 14:18:11 +0200 writes:

    Arne> Hi *,

    Arne> thanks for all your answers and discussions. And
    Arne> additionally special thanks to Henrik Bengtsson for
    Arne> writing the first draft of the "R Coding
    Arne> Conventions". I think that this document contains a
    Arne> lot of good ideas to make the code more
    Arne> readable. Since my package is *new* code, I adjusted
    Arne> it according to most recommendations of the RCC.

    Arne> However, one thing is still unclear to me: According
    Arne> to the RCC I gave the class of result of my function
    Arne> "linProg" also the name "linProg", but the RCC says
    Arne> that classes must start with uppercase, while
    Arne> functions must start with lowercase, which is
    Arne> contradictory in this case. In one of the examples of
    Arne> the RCC, a function that returns an object with a
    Arne> class attribute starts with uppercase:

    >> Line <- function(x0, y0, x1, y1) {
    >>      line <- list(x=c(x0,y0), y=(x1,y1));
    >>      class(line) <- "Line";
    >>      line;
    >> }

do not end lines with ";" in S (i.e. R or S-plus); it's
superfluous and considered ugly by many (incl. me) and teaches
(by example) a wrong idea.

    Arne> Does this mean that the names of these functions
    Arne> should start with uppercase?

While I agree that Henrik has put up several well thought out
recommendations {and very helpful postings such as the one you
cite below!} , these are *Henrik*'s recommendations and are
still subject to discussion and feedback.
The S language has quite a long tradition and existing function
and class base which cannot be changed mostly for compatibility
reasons.
One thing in this tradition is to have function "foobar" return
objects of class "foobar" (identical spelling including case).
This particularly applies to the "old" or S3 class/method system
on which still very much of the basic S models are based.
When using S3 classes (as you are above), I'd definitely keep
that S tradition.

This naming scheme can well change when the S4 class/methods
system is used (as it is more and more), since there, you
construct objects rather by   
  new("<classname>",....),
  as(obj, "<classname>", ....)
etc.

Regards,
Martin Maechler <maechler at stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
Seminar fuer Statistik, ETH-Zentrum  LEO C16	Leonhardstr. 27
ETH (Federal Inst. Technology)	8092 Zurich	SWITZERLAND
phone: x-41-1-632-3408		fax: ...-1228			<><


    Arne> On Friday 26 September 2003 17:43, you wrote:
    >> Hi, it looks from the names of your argument that your function is a
    >> "plain function", i.e. it is not a function specific to a class. If this
    >> is true, I would avoid the period and rename your function to
    >> 
    >> solveLP <- function(cvec, bvec, Amat, maximum, maxiter, verbose) ...
    >> 
    >> Under the S3 style of programming with classes methods coupled to
    >> classes are written in the format
    >> 
    >> method.class <- function(object, arg1, arg2, ...
    >> 
    >> That is, the part before the period is the name of a generic function
    >> and the part after is the name of the class. This is why R CMD check
    >> believe your that you have written a method 'solve' for class 'LP'. All
    >> methods named 'solve' should have a argument signature that match the
    >> generic function 'solve' and your solve.LP doesn't. I do not think this
    >> was your intention, correct? See help.start() -> "R Language Definition"
    -> "Object-oriented programming:" for more details about the S3 style.
    >> 
    >> To avoid problems like these I am working on a R Coding Conventions
    >> (RCC), http://www.maths.lth.se/help/R/RCC/ (see Naming Conventions). It
    >> is an early version and not everyone agrees with it, but the intention
    >> is to find a style that avoid problems like yours, where it says that
    >> you should avoid periods in function names except if you use it for S3
    >> class methods. Feedback is appreciated.
    >> 
    >> Cheers
    >> 
    >> Henrik Bengtsson
    >> Lund University
    >> 
    >> > -----Original Message-----
    >> > From: r-help-bounces at stat.math.ethz.ch
    >> > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Arne Henningsen
    >> > Sent: den 26 september 2003 17:03
    >> > To: r-help at stat.math.ethz.ch
    >> > Subject: [R] checking generic/method consistency
    >> >
    >> >
    >> > Hi,
    >> >
    >> > I wrote a package for linear programming and want to submit
    >> > it to CRAN.
    >> > Since the package 'quadprog' has a function with the name
    >> > 'solve.QP' to
    >> > perform Quadratic Programming, I named my (main) function 'solve.LP'.
    >> > However 'R CMD check' gives one warning:
    >> >
    >> > * checking generic/method consistency ... WARNING
    >> > solve:
    >> >   function(a, b, ...)
    >> > solve.LP:
    >> >   function(cvec, bvec, Amat, maximum, maxiter, verbose)
    >> >
    >> > while 'R CMD check' gives no warnings when the function has the name
    >> > 'solve.QP'.
    >> >
    >> > What do you recommend me to do?
    >> > 1) Ignore the warning and upload the package to CRAN as it is?
    >> > 2) Rename the function? (any suggestions?)
    >> > 3) Change something that avoids this problem without renaming
    >> > the functions?
    >> >
    >> > I would prefer the third point, but I don't know how.
    >> >
    >> > Thank you for your answers,
    >> >
    >> > Arne
    >> >
    >> > --
    >> > Arne Henningsen
    >> > Department of Agricultural Economics
    >> > Christian-Albrechts-University Kiel 24098 Kiel, Germany
    >> > Tel: +49-431-880-4445
    >> > Fax: +49-431-880-1397
    >> > ahenningsen at email.uni-kiel.de
    >> > http://www.uni-> kiel.de/agrarpol/ahenningsen.html
    >> >
    >> >
    >> >
    >> > ______________________________________________
    >> > R-help at stat.math.ethz.ch mailing list
    >> > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help

    Arne> -- 
    Arne> Arne Henningsen
    Arne> Department of Agricultural Economics
    Arne> Christian-Albrechts-University Kiel
    Arne> 24098 Kiel, Germany
    Arne> Tel: +49-431-880-4445
    Arne> Fax: +49-431-880-1397 
    Arne> ahenningsen at email.uni-kiel.de
    Arne> http://www.uni-kiel.de/agrarpol/ahenningsen/

    Arne> ______________________________________________
    Arne> R-help at stat.math.ethz.ch mailing list
    Arne> https://www.stat.math.ethz.ch/mailman/listinfo/r-help




More information about the R-help mailing list