title: clusterExport, enviroment and function scoping
body: <p>im new in parallel computing with R, im using the parallel library. I have several functions that are currently located in the global env and a function called main_fun, which call all of these functions. But i get an error that some functions from another library is not define, depsite the fact that i declared within the clusterExport</p>
  <pre><code>fun1 &lt;- function(data, pob){
       pob_n &lt;- ifelse(pob == 1, T, F)
       weights  &lt;- parSapply(cl, 1:nrow(pob_n), function(comb) data[pob_n[comb, ], &quot;wei&quot;] %&gt;% sum())
       punt &lt;- parSapply(cl, 1:nrow(pob_n), function(comb) data[pob_n[comb, ], &quot;Pun&quot;] %&gt;% sum())
       result &lt;- cbind(weights, punt, pob_n)  %&gt;% as.data.frame()
       return(result) 
  }
  
  main_fun &lt;- function(data) {
    n &lt;- nrow(data)
    N &lt;- 200
  
    cl &lt;- makeCluster(detectCores() - 0) # Generando el cluster r
    clusterExport(cl, list(&quot;n&quot;, &quot;N&quot;, &quot;data&quot;, &quot;fun1&quot;), envir = environment()) 
    clusterEvalQ(cl, {library(tidyverse)
                     library(parallel)}) 
    
    pob &lt;- parSapply(cl, 1:N, function(iteration) sample(c(0, 1), n, replace = T)) %&gt;% t() 
    gen_0 &lt;- fun1(data= data, pob= pob)
    stopCluster(cl) 
    return(gen_0 * 10)
  }
  
  </code></pre>
  <p>i get the following error:</p>
  <pre><code> Error in parSapply(cl, 1:nrow(pob), function(comb) data[pob[comb,  : 
    object 'cl' not found
  </code></pre>
  <p>what could be wrong?, i've just tried to pass the &quot;cl&quot; variable in the clusterExport function, and i still get the same error. The only solution that i had found is to put all the functions inside the main function, but i still want to know what its going on.</p>