title: R function for obtaining a reference to a variable
body: <p>In <a href="http://adv-r.had.co.nz/Environments.html" rel="nofollow noreferrer">Advanced R</a>, environments are advertised as a useful way to get pass-by-reference semantics in R: instead of passing a list, which gets copied, I can pass an environment, which is not. This is useful to know.</p>
  
  <p>But it assumes that whoever is calling my function is happy to agree on an "environment"-based data type, with named slots corresponding to the variables we want to modify.</p>
  
  <p>Hasn't someone made a class which allows me to just refer to a single variable by reference? For example,</p>
  
  <pre><code>v = 1:5
  r &lt;- ref(v)
  (function() {
      getRef(r)       # same as v
      setRef(r, 1:6)  # same as v &lt;&lt;- 1:6, in this case
  })()
  </code></pre>
  
  <p>It would seem to be pretty easy to do this, by storing the character name of v together with the environment where it is bound.</p>
  
  <p>Is there a standard library which accomplishes this semantics, or can someone provide a short snippet of code? (I haven't finished reading "Advanced R"; apologies if this is covered later in the book)</p>