title: parallelize call to old R projects using source
body: <p>I am in the process of updating old R projects. In this process I need executed old projects and new projects. </p>
  
  <p>I have manage to run old projects sequentially (one after another) and I am trying to do the same thing in parallel. </p>
  
  <p>These scripts do not return any value because they write into a database. </p>
  
  <h1>This is the working code</h1>
  
  <pre><code>execute_script &lt;- function(script_name, folder_path, required_data){
    #' This function prepares the input for a folder and a given script
    #' @param script_name: Contains the name of the executable .R script to be executed
    #' @param folder_path: Contains the folder where the script to executed is stored
    #' @param required_data: This contains the data required to run the script. 
    #' @return when finished returns TRUE to ensure it has ended 
  
    # I need to do this to join path on server with filename (it depends on user selection)
    script_to_execute &lt;- paste0(folder_path,"\\", script_name)
    script_to_execute &lt;- paste0((unlist(strsplit(script_to_execute, split='\\', fixed = TRUE))), collapse = "/")
  
    tryCatch( 
      expr = {    
        source(script_to_execute, local = TRUE, echo = TRUE, print.eval = TRUE, spaced = TRUE, chdir = TRUE)
      }, 
      error = function(e){
  
        print(paste(Sys.time(),"Excepcion execute_script: ",script_name))
        print(paste(Sys.time(),"Excepcion execute_script: ",e))
        return(FALSE)      
      }
    )
    return(TRUE) # TODO chech
  }
  
   model_execution_sequence &lt;- function(script_required_data, selected_scripts){
    #' Executes the scripts received in selected_projects using script_required_data
    #' @param script_required_data contains a dataframe with the input
    #' 
    #' @param selected_scripts is a datatable that contains all the information required to execute a script
    #'                    
    #' @return a boolean information to determine if execution was success or not 
  
  
    TYPE_1_MODEL &lt;- selected_scripts[APPROACH == '1']
    if (nrow(TYPE_1_MODEL) &gt; 0) {
  
  
      my_iters &lt;- 1:dim(TYPE_1_MODEL)[1]
      for (x in my_iters) {
        cat("\n\n")
        a &lt;- execute_script(TYPE_1_MODEL$FILE[x],
                              TYPE_1_MODEL$PATH[x],
                              script_required_data)
  
        print(paste(Sys.time(),"Executed",TYPE_1_MODEL$FILE[x],"result:",a))
      }
  
    }else{
      print(paste("Error: Unexpected approach named: ",selected_scripts," "))
  
    }
    return(TRUE)
  }
  </code></pre>
  
  <h1>This is the code I have tested with parallelization</h1>
  
  <pre><code>model_execution_foreach &lt;- function(script_required_data, selected_models){
    #' Executes the scripts received in selected_projects using script_required_data
    #' @param script_required_data contains a dataframe with the input
    #' 
    #' @param selected_scripts is a datatable that contains all the information required to execute a script
    #'                    
    #' @return a boolean information to determine if execution was success or not 
  
    TYPE_1_MODEL &lt;- selected_scripts[TYPE == '1']
  
    if (nrow(TYPE_1_MODEL) &gt; 0) {
      TYPE_1_MODEL &lt;- distinct(TYPE_1_MODEL)
  
  
      no_cores &lt;- detectCores() - 1
      cl &lt;- makeCluster(no_cores, type = "PSOCK")
  
      foreach (i = 1:dim(TYPE_1_MODEL)[1] ) %dopar%{
        ml&lt;- data.frame(execute_script(TYPE_1_MODEL$FILE[i], TYPE_1_MODEL$PATH[i], script_required_data))
      }
      stopCluster(cl)
  
    }else{
      print(paste("Error: Unexpected TYPE named: ",selected_scripts))
  
    }
    return(TRUE)
  }
  </code></pre>
  
  <h3>Reason to say it does not work</h3>
  
  <ol>
  <li>The script doesn't break (I put it inside a tryCatch)</li>
  <li>Each script should write data into the database and they are not</li>
  </ol>
  
  <p>I have followed links like:
  <a href="https://www.rdocumentation.org/packages/parallelize.dynamic/versions/0.9-1/topics/parallelize" rel="nofollow noreferrer">parallelize function</a></p>
  
  <p><a href="http://www.trutschnig.net/r_parallel.html" rel="nofollow noreferrer">Parallelism in R</a></p>
  
  <p>Thanks in advance</p>