[Rd] Formula evaluation, environments and attached packages

Milan Bouchet-Valat nalimilan at club.fr
Wed Apr 29 18:57:29 CEST 2015


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



More information about the R-devel mailing list