Re: [Rd] confusion over S3/S4 importing

From: Thomas Lumley <tlumley_at_uw.edu>
Date: Wed, 01 Aug 2012 08:12:51 -0700

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

______________________________________________
R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Received on Wed 01 Aug 2012 - 15:28:02 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 01 Aug 2012 - 16:30:34 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.

list of date sections of archive