I want to run some function in parallel that have effectively two outputs. I am fully aware that R function can only return one variable at a time. My problem is set in a R6 class, where I run a parallel loop using parLapply calling R6 class methods that change a variable inside the class instance as well as returning values to parLapply. However, any change made to class instance variables is not persistent, because the parallel loop is working with copies of my instance.
I understand that the instances are copied across forks and they cannot write to the same instance variable (regressionparameters) and therefore the instance variable does not change at all.
My instance variable should effectively be something like the list returned by parLapply.
library(R6)
library(parallel)
cl=makeForkCluster(2)
setDefaultCluster(cl)
lmclass=R6Class(
public=list(
regressionparameters=NULL,
fit = function(ix) {
lmfit=lm(mpg ~ hp, data = mtcars[ix,])
self$regressionparameters=rbind(self$regressionparameters,data.frame(t(coef(lmfit))))
return(fitted(lmfit))
}))
fitter=R6Class(
public = list(
initialize = function(model,ix) {
private$ix = ix
private$model=model
},
fitallp = function() { #parallel version
parLapply(cl = NULL,private$ix,model$fit)
},
fitalls=function(){ #serial version
lapply(private$ix,model$fit)
}
),
private = list(
model=NULL,
ix = NULL
)
)
model=lmclass$new()
test=fitter$new(model=model,ix=list(1:10,2:11,3:12))
preds=test$fitallp()
print(model$regressionparameters) #this happens as expected
NULL
preds=test$fitalls()
print(model$regressionparameters) #this is what I want
X.Intercept. hp
1 26.57124 -0.05049867
2 26.30332 -0.05038933
3 26.37547 -0.05175793
stopCluster(cl)
Returning a list with both output variables inside the parLapply and then merging the respective items and assigning it to model$regressionparameter is not really a solution as a require as the types of model classes my fitter can use can vary and could have an arbitrary number of outputs and could contain similar classes with their own regressionparameters.