Skip to content

import question

6 messages · Brian Ripley, Ben Bolker, Hadley Wickham

#
I have been struggling all day to import a particular function/method
combination (ranef(), which extracts the random effects from a mixed
model fit) from the nlme package into another package ... so far without
success.

  The NAMESPACE for nlme contains the following lines:

export(..., ranef, ...)
S3method(ranef, lme)

  ranef is defined as a standard S3 generic,

function (object, ...)
   UseMethod("ranef")
<environment: namespace:nlme>

  I made up a minimal package, "raneftest" (which is available at
<http://www.math.mcmaster.ca/~bolker/misc/raneftest_0.001.tar.gz>)
that contains a single function, ranef.x(), which is supposed to be an
S3 method for class x ... its (trivial) definition is

ranef.x <- function(object, ...) {
   print("x")
}

the package has a NAMESPACE file:

-----------
exportPattern("^[^\\.]")

##importFrom(nlme,ranef)
import(nlme)
----------

(I've tried both import() and importFrom() -- I would prefer to use
importFrom() if I can ... I also tried importMethodsFrom(nlme,ranef) ,
but it fails -- it seems to be intended for S4 and not S3 methods?)

  The package also has a tests directory with a single file, which looks
like this:

  > library(raneftest)
  > x <- 1
  > class(x) <- "x"
  > ranef.x(x)
  [1] "x"
  > ranef(x)
  Error: could not find function "ranef"
  Execution halted

  I have read the relevant section of R-exts.pdf several times --
apologies in advance if I am doing something boneheaded.

  Does anyone have suggestions for what to try next ... ?

  cheers
    Ben Bolker
#
Ben Bolker <bbolker <at> gmail.com> writes:
Answered my own question, finally.

  Apparently an explicit

export(ranef)

is required, even though there is also an

exportPattern("^[^\\.]")

in the NAMESPACE file, which I would have thought would
export 'ranef' along with everything else ... ?
#
On Thu, 24 Mar 2011, Ben Bolker wrote:

            
It exports everything excpet dot-namesin the package's namespace. 
Imports are not in the namespace per se, but in the import environment 
(which is the enclosure of the namespace).  Here is the actual code:

         for (p in nsInfo$exportPatterns)
             exports <- c(ls(env, pattern = p, all.names = TRUE), exports)

So to re-export a function, you need to do so explicitly.

It is consdered good practice not to use exportPattern() (at least, 
not for broadly defined patterns) in production code: see the current 
'Writing R Extensions'.  Otherwise things may change under you (what 
nlme exports has changed recently, hence what import(nlme) brings it 
has).
#
Prof Brian Ripley <ripley <at> stats.ox.ac.uk> writes:
Thank you.
  I will transition away from using exportPattern (although it seemed
like a good quick and dirty solution to this testing problem).

  Please consider the following patch to R-exts ...

  thanks
    Ben Bolker

===================================================================
--- R-exts.texi	(revision 55002)
+++ R-exts.texi	(working copy)
@@ -2310,7 +2310,9 @@
 package @pkg{foo} are to be imported.
 
 It is possible to export variables from a name space that it has
-imported from other name spaces.
+imported from other name spaces (they need to be exported 
+using an explicit @code{export} directive;
+i.e. @code{exportPattern} will not work).
 
 If a package only needs a few objects from another package it can use a
 fully qualified variable reference in the code instead of a formal
#
On Wed, Mar 23, 2011 at 7:20 PM, Ben Bolker <bbolker at gmail.com> wrote:
But why do you want to export this method out of your package? (If you
export it, you will need to document it) Can't you rely on nlme being
loaded?

i.e. your test should be

library(nlme)
library(raneftest)
x <- 1
class(x) <- "x"
ranef.x(x)
ranef(x)


Hadley
#
On 03/24/2011 03:03 PM, Hadley Wickham wrote:
nlme and lme4 do not play nicely together (this may be
fixable/eventually be fixed, but it's the current state of affairs), and
I want this package to interoperate with lme4.  Thus I want to avoid
explicitly loading nlme.
  Sorry I didn't make this clear initially (someone else asked about
this off-list).


  cheers
    Ben