问题
I'm having an issue on R6 classes when used with foreach() together, possibly to do with environments (I'm using Windows).
Suppose that there are two R6 classes, "class1" and "class2". method1 in class1 is dependent on class2 (see example code below for example).
The issue is, if I use foreach() %dopar%
on class1, R doesn’t seem to recognise class2, even if I set .export = c("class1", "class2")
explicitly in foreach()
statement. (Here class1 uses class2)
However if I use foreach()
on class2, it works fine… (Here I just use class2 within foreach()
)
So the problem seems to be that, if class2 is “nested” within class1, then class2 will not work with foreach()
. I’m feeling like this is to do with environment, but can’t figure out how. I even tried .export = ls(.GlobalEnv)
but it still doesn’t work…
I can get around this by instantiate an object from class2 and use it as an extra parameter in method1, e.g. method1 = function(input = 1:3, objectFromClass2)
when defining class1, but it may not be an optimal solution in the long run - especially considering code maintainability and ease of debug as the priorities (and that's the reason I'm using R6's OO feature anyway).
Many thanks in advance!
Here is an example of code:
cl = makeCluster(3)
registerDoParallel(cl)
class1 = R6Class(
"class1",
public = list(
method1 = function(input = 1:3){
y = class2$new()
output = y$method2(input)
return (output * 3)
}
)
)
class2 = R6Class(
"class2",
public = list(
method2 = function(input) {
return (input + 1)
}
)
)
# This doesn’t work.
# Error in { : task 1 failed - "object 'class2' not found"
foreach(input = 1:3, .packages = "R6", .export = c("class1", "class2")) %dopar% {
y = class1$new()
z = y$method1(input)
return (z)
}
# This works
foreach(input = 1:3, .packages = "R6", .export = c("class1", "class2")) %dopar% {
y = class2$new()
z = y$method2(input)
return (z)
}
# Class1 also works fine if it’s called outside of foreach()
y = class1$new()
z = y$method1(1:3)
来源:https://stackoverflow.com/questions/49435632/issue-on-using-r6-classes-and-foreach-dopar-together