'doParallel does not recognize functions in global environments with multiple sourcings
I have a quick question with respect to the doParallel
package in R. I have an optimize.R
file where it contains roughly 18 functions A1
, A2
, A3
, A4
, ..., A18
within it, and A18
would basically contain all the functions A1
, A2
, A3
,..., A17
. I have another result.R
file, where I used import every functions within optimize.R
into the Global Environment of RStudio (located on the upper-right corner of RStudio). In addition, within result.R file, I have a function F
that calls function E
under the loop foreach(..., .export = c(".GlobalEnv")) %dopar% {do something}.
Now, in the final.R file (which is the main file), I use source(/C:/Users/[Name]/Desktop/result.R
, echo = F). Within final.R, I have a function res
that calls function F.
However, when I execute this final.R file, I got the error message: task 1: cannot find variable C
, which is weird because C
is a function, not a variable. Also, it seems to me the .export = c(".GlobalEnv")
fails to work, as function C
is already in the Global Environment. Can anyone suggest some ways to overcome this kind of issue? I tried to export
Flow of the 3 files.
final.R file
source(/C:/Users/[Name]/Desktop/result.R, echo = F)
library(doParallel)
registerDoParallel(4)
res <- function(var_x, var_y) {
best.rev <- foreach(i=1:5, .export = c(".GlobalEnv")) %dopar% {
F(var_x, var_y)
do something else
}
output <- best.rev
}
result.R file
source(/C:/Users/[Name]/Desktop/optimize.R, echo = F)
F <- function(var1, var2) {
E(var1, var2)
do something else
}
optimize.R file
A1 <- function(x1, x2) {....}
A2 <- function(x1, x3, x4) {...}
A3 <- function(x1, x2, x3) {...}
....
A18 <- function(x1,x2,x3,x4,...,x8) {
a1 <- A1(x1, x2)
a2 <- A2(a1, x3, x4)
a3 <- A3(a1, a2, x3)
return(list(a1, a2, a3))
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|