title: Export manually missed globals object with future
body: <p>I defined a function <code>f</code> in a package that takes data and an R expression as input and then applies the user-defined expression on the data. Here's an example of the function's use:</p>
  
  <pre><code>f &lt;- function(data, expr) {
    expr &lt;- substitute(expr)
    eval(expr, envir = data)
  }
  
  data &lt;- data.frame(a = 1:2, b = 3:4)
  f(data, mean(a))
  #&gt; [1] 1.5
  </code></pre>
  
  <p>The problem arises with the parallel version of this function using explicit futures and user-defined object. Here a toy version:</p>
  
  <pre><code>library(future)
  
  f &lt;- function(data, expr) {
    expr &lt;- substitute(expr)
    y &lt;- future::future(eval(expr, envir = data))
    future::value(y)
  }
  
  data  &lt;- data.frame(a = 1:2, b = 3:4)
  myfun &lt;- function(x){sum(sqrt(x))}
  
  plan(sequential)
  f(data, myfun(a))
  #&gt; [1] 2.414214
  
  plan(multiprocess)
  f(data, myfun(a))
  #&gt; Error in myfun(a) : impossible to find function "myfun"
  </code></pre>
  
  <p>The problem is that <code>myfun</code> cannot trivially be found by <code>future</code> and thus must be exported manually. I'm able to fix this issue by analyzing <code>expr</code> with <code>future::getGlobalsAndPackages</code> and then manually adding objects:</p>
  
  <pre><code>future::future(..., globals = structure(TRUE, add = globals))
  </code></pre>
  
  <p>I'm wondering if there is a better/good way to do that since it looks like a hack to me.</p>