0

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.

ak17
  • 167
  • 1
  • 9
  • "My problem is set in a R6 class, where I run a parallel loop using parLapply calling R6 class...." - *could you please more explicitly state the problem itself? (and describe any desired output)* – Hack-R May 25 '18 at 19:13
  • the code was generating the output I forgot to include the output with the code. hope it is more clear now – ak17 May 25 '18 at 20:33
  • I would return everything and make the assignment after. – F. Privé May 25 '18 at 22:32
  • @F.Privé I need more flexibility than that. Maybe I do not do all fits in one go. The purpose of using the OO approach was so that each class does its own thing when fit is called and stores the parameters and also returns fitted values. – ak17 May 30 '18 at 13:22

0 Answers0