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

Martin Maechler maechler at stat.math.ethz.ch
Mon Apr 11 17:18:56 CEST 2005


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 at 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



More information about the R-devel mailing list