From bbolker at gmail.com  Mon Jul 30 16:12:01 2012
From: bbolker at gmail.com (Ben Bolker)
Date: Mon, 30 Jul 2012 10:12:01 -0400
Subject: [Rd] confusion over S3/S4 importing
Message-ID: <50169631.6020709@ufl.edu>


  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.

  Any insights?

  thanks
    Ben Bolker



[Rd] confusion over S3/S4 importing
Ben Bolker