You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I write %dofuture% inside a self-defined function, it can only get variables from global environment. Those variables from the local environment of the self-defined function are ignored.
Here is the example:
myFunction1=function(x){
y=x+1return(y)
}
myFunction2=function(x, y){
z=x+yreturn(z)
}
myFunction3=function(function_var4, function_var5, function_var6){
# Claim some local variableslocal_var1= vector("list", length= ncol(function_var4))
local_var2= vector("list", length= ncol(function_var4))
local_var3=function_var5 %>% pull(function_var6)
local_var4=data.frame(Var= seq(min(local_var3), max(local_var3), length.out=10000))
# Do some parallel calculation
plan(multisession, workers=parallel::detectCores() -2)
foreach (i=1:ncol(function_var4)) %dofuture% {
data_glm=data.frame(Var=local_var3,
PreAbs=function_var4[,i])
mod_glm= glm(PreAbs~ poly(Var, 3), family=binomial, data=data_glm)
# Result of the calculationlocal_local_var1= predict(mod_glm, newdata=local_var4, se=F, type="response")
# Some simple calculation using local_local_var1# Save the resultlocal_var1[[i]] = mean(local_local_var1) # <---- I guess this cause the errorlocal_var2[[i]] = myFunction1(local_local_var1)
}
# Close multisession workers by switching plan
plan(sequential)
local_var5= myFunction2(local_local_var1, function_var5)
return(list(opt= unlist(local_var1),
nw= unlist(local_var2),
miv= unlist(local_var5)))
}
I Get the error message Error in eval(quote({ : object 'local_var1' not found when run the code below:
When I write %dofuture% inside a self-defined function, it can only get variables from global environment. Those variables from the local environment of the self-defined function are ignored.
Here is the example:
I Get the error message Error in eval(quote({ : object 'local_var1' not found when run the code below:
I also posted the question on stack overflow: https://stackoverflow.com/questions/77772436/r-variable-scoping-issue-of-parallel-calculation-dofuture-in-a-nested-functi
The text was updated successfully, but these errors were encountered: