From maechler at stat.math.ethz.ch  Mon Apr 11 17:18:56 2005
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon Apr 11 17:19:04 2005
Subject: [Rd] exists("loadings.default") ...
In-Reply-To: <A6DA59168387AF408128AC9C7EBE521D17E7F6@BOC-EXMAIL1.bocad.bank-banque-canada.ca>
References: <A6DA59168387AF408128AC9C7EBE521D17E7F6@BOC-EXMAIL1.bocad.bank-banque-canada.ca>
Message-ID: <16986.38240.449928.740979@stat.math.ethz.ch>

Paul Gilbert asked me the following, about a topic that was
dealt here (on R-devel) a few weeks ago (~ March 21):

>>>>> "PaulG" == Paul Gilbert <pgilbert@bank-banque-canada.ca>
>>>>>     on Mon, 11 Apr 2005 10:35:03 -0400 writes:

    PaulG> Martin, a while ago you suggested:

    >> For S3, it's a bit uglier, but I think you could still do -- in your
    >> package --

    >> if(!exists("loadings.default", mode="function")) {
    >>   loadings.default <- loadings
    >>   loadings <- function(x, ...) UseMethod("loadings")
    >> }

    PaulG> I don't think exists works properly here if namespaces are used and
    PaulG> loadings.default is not exported. (i.e. it always gives false.) I can
    PaulG> redefine loadings and loadings.default, but I can't guard against the
    PaulG> possibility that those might actually be defined in stats someday.

Yes, you are correct, one cannot easily use exists() for this
when namespaces are involved.

For S3 methods, instead of exists(), I think one should use
something like

  > !is.null(getS3method("loadings", "default", optional = TRUE))
  [1] FALSE
  > !is.null(getS3method("predict", "ppr", optional = TRUE))
  [1] TRUE

Apart from the need to mention something along this line on
'exists' help page,  I wonder if we shouldn't even consider providing an  
existsS3method() wrapper, or alternatively and analogously to getAnywhere() an
existsAnywhere() function.

Martin


[Rd] exists("loadings.default") ...
Martin Maechler