[Rd] confusion over S3/S4 importing

Thomas Lumley tlumley at uw.edu
Wed Aug 1 17:12:51 CEST 2012


On Mon, Jul 30, 2012 at 7:12 AM, Ben Bolker <bbolker at gmail.com> wrote:
>
>   Can anyone help me figure out the right way to import a method that is
> defined as S3 in one package and S4 in another?
>
>   Specifically:
>
>  profile() is defined as an S3 method in the stats package:
>
> function (fitted, ...)
> UseMethod("profile")
> <bytecode: 0xa4cd6e8>
> <environment: namespace:stats>
>
>  In stats4 it is defined as an S4 method:
>
>  stats4:::profile
> standardGeneric for "profile" defined from package "stats"
>
> function (fitted, ...)
> standardGeneric("profile")
> <environment: 0xa59a364>
>
>    In the bbmle package I want to define it as an S4 method:
>
>   setMethod("profile", "mle2", function (...) {...})
>
>   In the NAMESPACE file for bbmle I have
>
>  importFrom(stats4,profile)
>
>   However, in R 2.14.2 (which is I am currently trying to fix: I would
> like to try to allow for backward compatibility), I get
>
> library(bbmle)
> example(mle2) ## to create some fitted objects
> profile(fit)
> Error in UseMethod("profile") :
>   no applicable method for 'profile' applied to an object of class "mle2"
>
>  whereas stats4::profile(fit) works fine.
>
>   -- a similar issue occurs for coef.
>
>   I would have thought that importFrom() would allow package code to
> find the 'stats4' version of profile (and coef) before it found the
> 'stats' versions , but that doesn't seem to be the case.
>
>   Just for kicks I tried
>
> import(stats4)
>
>   which leads to warning messages but no improvements.
>
> If I add an explicit Depends: on stats4 that fixes the problem. I may be
> forced to do that, but I thought it was supposed to be a last resort and
> that I was *supposed* to be able to fix my problems by proper use of
> imports.

'Imports' won't be enough -- the whole point of a generic is that it's
visible to the user, which doesn't happen with imports.

   -thomas

-- 
Thomas Lumley
Professor of Biostatistics
University of Auckland



More information about the R-devel mailing list