Defining methods that call other methods outside of R6 objects
<p>When using R6 classes, what is the proper way to define methods outside of the class that call other methods?</p>

<p>Consider the following example, where the function <code>func</code> might dispatch to another function if it is being used interactively.  But, if it does so, the other function doesn't have access to the private environment.  Should I be passing an environment around if I define classes this way?</p>

<pre><code>## General function defined outside R6 class
func &lt;- function(x) {
  if (missing(x) &amp;&amp; interactive()) {
    ifunc()
  } else {
    private$a &lt;- x * x
  }
}

## If interactive, redirect to this function
ifunc &lt;- function() {
  f &lt;- switch(menu(c('*', '+')), '1'=`*`, '2'=`+`)
  private$a &lt;- f(private$a, private$a)
}

## R6 test object
Obj &lt;- R6::R6Class("Obj",
  public=list(
    initialize=function(a) private$a &lt;- a,
    geta=function() private$a,
    func=func  # defined in another file
  ),
  private=list(
    a=NA
  )
)

## Testing
tst &lt;- Obj$new(5)
tst$func(3)
tst$geta()  # so func sees 'private'
# [1] 9

tst$func()  # doesn't see 'private'
</code></pre>

<blockquote>
  <p>Error in ifunc() (from #3) : object 'private' not found</p>
</blockquote>