can we modify an empty environment variable inside a parralled foreach?

19 Views Asked by At

I want to first pass an empty environment variable to a paralleled foreach in R and then modify the empty environment variable inside the paralleled foreach. I want the empty environment variable to be changed Not by the return of the foreach.

why did the following codes fail? And how can I achieve these two aims?

empty_env <- rlang::new_environment()

Define a variable in the empty environment

empty_env$my_variable <- 0

Use foreach with .export to pass the empty environment to parallel processes

foreach(i = 1:5, .export = "empty_env") %dopar% {

Access and modify the variable in the empty environment within the parallel process

empty_env$my_variable <- empty_env$my_variable + i

Print the modified value within the parallel process

print(empty_env$my_variable) }

Print the final value in the empty environment

print(empty_env$my_variable)

0

There are 0 best solutions below