From ht at heatherturner.net  Wed Apr 29 19:27:18 2015
From: ht at heatherturner.net (Heather Turner)
Date: Wed, 29 Apr 2015 18:27:18 +0100
Subject: [Rd] Formula evaluation, environments and attached packages
In-Reply-To: <1430326649.21653.144.camel@club.fr>
References: <1430326649.21653.144.camel@club.fr>
Message-ID: <1430328438.3742979.260277237.3AB262D7@webmail.messagingengine.com>

Hi Milan,

I expect I may be able to do something about the way the terms are
evaluated, to ensure the evaluation is done in the gnm namespace (while
still ensuring the variables can be found!).

In the meantime, I think the following will work:

Mult <- gnm::Mult
 f <- Freq ~ Eye + Hair + Mult(Eye, Hair)
gnm::gnm(f, family=poisson, data=dat)

Hope that helps,

Heather

On Wed, Apr 29, 2015, at 05:57 PM, Milan Bouchet-Valat wrote:
> Hi!
> 
> Some time ago, I replaced calls to library() with calls to
> requireNamespace() in my package logmult, in order to follow the new
> CRAN policies. But I just noticed it broke jackknife/bootstrap using
> several workers via package parallel.
> 
> The reason is that I'm running model replicates on the workers, and the
> formula includes non-standard terms like Mult() which are provided by
> gnm. If gnm is not attached to the global namespace, these functions are
> not found.
> 
> What would be the best solution to fix this? I tried changing the
> environment of the formula to be gnm's namespace, but so far I failed.
> 
> Here's a reproducer using only gnm:
> 
> > dat <- structure(c(326, 688, 343, 98, 38, 116, 84, 48, 241, 584, 909, 
> + 403, 110, 188, 412, 681, 3, 4, 26, 85), .Dim = 4:5, .Dimnames =
> structure(list(
> +     Eye = c("Blue", "Light", "Medium", "Dark"), Hair = c("Fair", 
> +     "Red", "Medium", "Dark", "Black")), .Names = c("Eye", "Hair"
> + )), class="table")
> > 
> > f <- Freq ~ Eye + Hair + Mult(Eye, Hair)
> > gnm::gnm(f, family=poisson, data=dat)
> Error in which(sapply(variables, function(x) { : 
>   error in evaluating the argument 'x' in selecting a method for function
>   'which':
> Error in get(as.character(FUN), mode = "function", envir = envir) : 
>   object 'Mult' of mode 'function' was not found
> 
> # Failed attempt by setting the environment
> > environment(f) <- loadNamespace("gnm")
> > environment(f)                        
> <environment: namespace:gnm>
> > gnm::gnm(f, family=poisson, data=dat)
> Error in which(sapply(variables, function(x) { : 
>   error in evaluating the argument 'x' in selecting a method for function
>   'which':
> Error in get(as.character(FUN), mode = "function", envir = envir) : 
>   object 'Mult' of mode 'function' was not found
> 
> 
> Thanks for your help



[Rd] Formula evaluation, environments and attached packages
Heather Turner