'rstudioapi::jobRunScript export results of multiple jobs in a specific environment

I am working with rstudioapi::jobRunScript to run a script in parallel while being able to continue to use the same Rstudio session.

While when I use exportEnv = "R_GlobalEnv" all my objects created by the scripts are correctly returned, when I use an environement I create muself "tmp.env", the only object I get is the last one:

tmp.env = new.env()
x <- 1
for(i in 1:3){
  file.create(paste0("test_job_script",i,".R"))
  cat(paste0("x",i,"<-", x,"+",i),
  file = paste0("test_job_script",i,".R"),
  append = TRUE)
  rstudioapi::jobRunScript(path = paste0("test_job_script",i,".R"),
                       importEnv = TRUE,
                       exportEnv = "tmp.env") # If "R_GlobalEnv" everything is ok   
} 

How could I manage to store all my results within a specific environnement? Thanks in advance for your help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source