[Rd] promptClass misses methods

Ross Boylan ross at biostat.ucsf.edu
Fri Dec 1 20:33:21 CET 2006


On Fri, Dec 01, 2006 at 11:37:46AM +0100, Martin Maechler wrote:
> >>>>> "RossB" == Ross Boylan <ross at biostat.ucsf.edu>
> >>>>>     on Thu, 30 Nov 2006 22:29:06 -0800 writes:
> 
>     RossB> I've had repeated problems with promptClass missing
>     RossB> methods, usually telling me a class has no methods
>     RossB> when it does.
> 
>     RossB> In my current case, I've defined an S4 class
>     RossB> "mspathCoefficients" with a print method
>     RossB> setMethod("print", signature(x="mspathCoefficients"),
>     RossB> function(x, ...)  { # etc
> 
> You should *not* define "print" methods for S4 classes;
> rather you should define "show" methods.

Is that because print is used by the S3 system?  And is the general
rule to avoid using S3 methods for S4 classes?  For example,
http://www.omegahat.org/RSMethods/Intro.pdf, which is referenced in
the package help for methods, discusses show, print and plot as 3
alternatives in S4 (p. 9, though a footnote says that at that
time--2001--R didn't recognize formal methods for printing objects.)

I've been unable to locate much information about combining S3 and S4
methods, though I recall seeing a note saying this issue was still to
be addressed in R.  Perhaps it has been now, with setOldClass?  At any
rate, the help for that method addresses classes rather than methods,
and I didn't see anything in ?Methods, ?setMethod, or ?setGeneric.

show() raises two additional issues for me.  First, it takes a single
argument, and I want to be able to pass in additional arguments via
... .  Second, I read some advice somewhere, that I no longer can find,
that show methods should return an object and that object in turn
should be the thing that is printed.  I don't understand the
motivation for that rule, at least in this case, because my object is
already a results object.

> 
>     RossB> The file promptClass creates has no methods in it.
>     >> showMethods(classes="mspathCoefficients")
>     RossB> Function: initialize (package methods)
>     RossB> .Object="mspathCoefficients" (inherited from:
>     RossB> .Object="ANY")
> 
> so it's just inherited from "ANY"
> 
>     RossB> Function: print (package base) 
>     RossB> x="mspathCoefficients"
> 
> that's the one

So why isn't promptClass picking it up?

> 
>     RossB> Function: show (package methods)
>     RossB> object="mspathCoefficients"
>     RossB>  (inherited from: object="ANY")
> so it's just inherited from "ANY"
> 
> Ross, it would really be more polite to your readers if you
> followed the posting guide and posted complete
> fully-reproducible code...

I thought it might be overkill in this case.  At any rate, it sounds
as if I may be trying to do the wrong thing, so I'd appreciate
guidance on what the right thing to do is.

Here's a toy example:
setClass("A", representation(x="numeric"))
setMethod("print", signature(x="A"), function(x, ...) print(x at x, ...) )
promptClass("A")

The generated file has no print method.
 
> 
>     >> getGeneric("print")
>     RossB> standardGeneric for "print" defined from package
>     RossB> "base"
> 
>     RossB> function (x, ...)  standardGeneric("print")
>     RossB> <environment: 0x84f2d88> Methods may be defined for
>     RossB> arguments: x
> 
> 
>     RossB> I've looked through the code for promptClass, but
>     RossB> nothing popped out at me.
> 
>     RossB> It may be relevant that I'm running under ESS in
>     RossB> emacs.  However, I get the same results running R
>     RossB> from the command line.
> 
>     RossB> Can anyone tell me what's going on here?  This is
>     RossB> with R 2.4, and I'm not currently using any namespace
>     RossB> for my definitions.
> 
> [and not a package either?]
The code is part of  a package, but I'm developing code snippets in
ESS without loading the whole package.
> 
> I'm very willing to look at this, once
> you've provided what the posting guide asks for, see above.
> 
> Regards,
> Martin

Thank you.  For completeness, here's some system info:

> sessionInfo()
R version 2.4.0 (2006-10-03) 
i486-pc-linux-gnu 

locale:
LC_CTYPE=en_US;LC_NUMERIC=C;LC_TIME=en_US;LC_COLLATE=en_US;LC_MONETARY=en_US;LC_MESSAGES=en_US;LC_PAPER=en_US;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=en_US;LC_IDENTIFICATION=C

attached base packages:
[1] "methods"   "stats"     "graphics"  "grDevices" "utils"     "datasets" 
[7] "base"     

The Debian package is r-base-core 2.4.0.20061103-1.

Ross



More information about the R-devel mailing list