'R library MuMin dredge function not finding data when called from inside a function in parellel mode

I'm trying to call the MuMIN dredge function in parallel mode from inside a function that sets up the data for the model. Even if I pass the data to the clusters with clusterExport, dredge seems to be looking for the data in the global environment. Here is some code with toy data:

library(MuMIn)
library(parallel)
dataGlobal<-data.frame(x=rnorm(20),y=rnorm(20))
cl2<-makeCluster(4)
setDefaultCluster(cl2)

#This works
lm1<-lm(y~x,data=dataGlobal,na.action=na.fail)
clusterExport(cl2,varlist=list("dataGlobal","lm1"),envir = environment())
lm2<-MuMIn::dredge(lm1,cluster=cl2)
lm2

#But not if I put it in a function
func1<-function(dataFunction) {
  lmf1<-lm(y~x,data=dataFunction,na.action=na.fail)
  clusterExport(cl2,varlist=list("dataFunction","lmf1"),envir = environment())
  lmf2<-MuMIn::dredge(lmf1,cluster = cl2)
  lmf2
}
func1(dataGlobal)
# Returns: Error in nobs(global.model) : object 'lmf1' not found

#The function only works if I move the data from the function to the global environment
func2<-function(dataFunction) {
  lmf1<-lm(y~x,data=dataFunction,na.action=na.fail)
  assign("lmf1",lmf1,envir=globalenv())
  assign("dataFunction",dataFunction,envir=globalenv())
  clusterExport(cl2,varlist=list("dataFunction","lmf1"),envir = environment())
  lmf2<-MuMIn::dredge(lmf1,cluster = cl2)
  lmf2
}
func2(dataGlobal)
#works

How can I give the data from the function's environment to dredge in the clusters without copying it to the global environment? Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source