title: Use multi-core in LINUX to run R-script
body: <p>I have a function called "my_function" and I want to run in parallel using mcapply in in LINUX using multi-cores. I have 10 cty_id's 20 years to run for each cty_id. How can I use mcapply to make this fast run using 4 cores? I have tested my function to run one county and one year at a time and it is working fine. But, I want to speed up the process rather than manually changing the years and cty_id one at a time.</p>
  
  <pre><code>cty_id &lt;- c(205,15,37,59,25,133,11,23,21,19)
  val_yr &lt;- c(1998:2017)
  
  my_function &lt;- function(cty_id,val_yr) {
  
  &lt;do something here&gt; ()
  
  }
  </code></pre>
  
  <p>I have my code as follows but it does not do the job and crashes.</p>
  
  <pre><code>library("parallel")
  mcapply(c(205,15,37,59,25,133,11,23,21,19),FUN=my_function, val_yr=years[1998:2017], 4L)
  </code></pre>
  
  <p>Can somebody help me to run this faster? </p>
  
  <p>Please let me know what global variables I need to define.</p>
  
  Revised R. code file below
  
  <pre><code>in_file11 &lt;- 'PLSS_KS_All_1999_2017.txt' 
  in_file12 &lt;- 'PLSS_KS_All_WeeklySM_1998_2017_BILINEAR.txt' 
  in_file13 &lt;- 'PRISM_WeeklyPrcp_Sum_800m_1998_2017_BILINEAR.txt'
  
  in_data11 &lt;- fread(in_file11,drop = 1)
  in_data12 &lt;- fread(in_file12,drop = 1)
  in_data13 &lt;- fread(in_file13,drop = 1)
  
  in_datan &lt;- as.data.table(full_join(in_data12, in_data13))
  in_data1 &lt;- as.data.table(full_join(in_data11, in_datan))
  
  in_file2 &lt;- 'KS_pp_Wheat_hist_YieldID_1998_2017.csv' 
  in_file3 &lt;- 'All_counties_1999_2017.csv'
  
  in_data2 &lt;- fread(in_file2)
  in_data3 &lt;- fread(in_file3)
  
  years &lt;- c(1998:2017)
  st_id &lt;- c(15)  
  crop_id &lt;- c(11)
  
  my_function &lt;- function(cty_id,val_yr) {
  
  &lt;do something here&gt; ()
  
  }
  
  
  registerDoFuture()
  plan(multiprocess)
  num.cores &lt;- detectCores()-1
  cluztrr &lt;- makeCluster(num.cores)
  registerDoParallel(cl = cluztrr)
  
  plan(cluster, workers = cluztrr)
  
  
  county_id &lt;- c(19,205)
  val_year &lt;- c(1998:1999)
  
  foo &lt;- expand.grid(county_id,val_year)
  
  
  foreach(i = 1:nrow(foo), globals = c("in_data1","in_data2","in_data3"), .export = c("years","st_id","crop_id")) %dopar% {
    my_function(foo[i,]$Var1,foo[i,]$Var2)
  }
  
  stopCluster(cluztrr)
  
  Error in { : task 1 failed - "object 'in_data1' not found"
  In addition: Warning message:
  In e$fun(obj, substitute(ex), parent.frame(), e$data) :
    already exporting variable(s): st_id, crop_id
  </code></pre>