From bbolker at gmail.com  Fri Jun 28 05:49:45 2013
From: bbolker at gmail.com (Ben Bolker)
Date: Thu, 27 Jun 2013 23:49:45 -0400
Subject: [Rd] problem with eval(...,
	parent.frame(1L)) when package is not loaded
Message-ID: <51CD07D9.7070201@ufl.edu>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


  The lmer() function in the lme4 package has some code of the form

mc <- match.call()
mc[[1]] <- as.name("lFormula")
lmod <- eval(mc, parent.frame(1L))

  this is a fairly common idiom in R, found e.g. in lm(), used when
one wants to pass all of the arguments of a function to a different
function (in the case of lm() it's model.frame()).

  This idiom fails ("error in eval(...) : could not find function
'lFormula'") when lme4 is not explicitly loaded, as will happen if one
uses lme4::lmer(...) to call the function without loading the package,
or when lmer() is called from within a function that Imports: but does
not Depend: on lme4.

  A simpler version suggested by Martin Maechler shows that this is a
generic issue (except that it's unlikely to happen with base R
functions because the relevant packages _will_ be loaded unless one
bends over backwards):

R_DEFAULT_PACKAGES=NULL R --vanilla
> stats::lm(y~x,data=data.frame(1:5,1:5))
Error in eval(expr, envir, enclos) :
  could not find function "model.frame"

  I wonder if anyone has encountered this and/or can think of a fix
(something with setting the enclosing environment appropriately?)  The
only workarounds we can think of at present are (1) telling
dependent-package maintainers that they have to use Depends: instead
of Imports: (which seems to subvert the whole dependency-minimization
goal of Imports:) or (2) littering our code with lme4:: to make sure
that the function gets looked for in the right place (ugh).

  See also https://github.com/lme4/lme4/issues/50 .

  Any ideas appreciated.

  Ben Bolker

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with undefined - http://www.enigmail.net/

iQEcBAEBAgAGBQJRzQfZAAoJEOCV5YRblxUH3d0H+wfIa3ZJICkgt+4O6hsCPf3h
uMs5aQqZZl37W+8vOx4P6FBYb2dc96SGZhYmyYude9wokHnCx4E+GI34YJkASST0
v8BXp7kdk2kSXS+FBzwbxbIaUFlS44nzv/fA0zaVfa1YbsfWQaxKF2p/BLu+gxK8
pCdVWcTqN6rLsO2i9nPGG90W4zCo3mVx/83D4vbSXo17bYMuSrOd5OGUE9NRJz2G
gJLqtH9DC+NbCbAO47Sa1/od7ABiRD2Cl8yN+fmNHQOiXYB50IrCA8kCNk10zNnT
lZ/u90TPS2gHV0dF9MYPGgjp8GYpycXzdVt9u4idLOtYOOS7lBA5792jc0wrGoU=
=oBtv
-----END PGP SIGNATURE-----



[Rd] problem with eval(..., parent.frame(1L)) when package is not loaded
Ben Bolker