title: Passing a variable name to a function in R
body: <p>I've noticed that quite a few packages allow you to pass symbol names that may not even be valid in the context where the function is called. I'm wondering how this works and how I can use it in my own code?</p>
  
  <p>Here is an example with ggplot2:</p>
  
  <pre><code>a &lt;- data.frame(x=1:10,y=1:10)
  library(ggplot2)
  qplot(data=a,x=x,y=y)
  </code></pre>
  
  <p><code>x</code> and <code>y</code> don't exist in my namespace, but ggplot understands that they are part of the data frame and postpones their evaluation to a context in which they are valid. I've tried doing the same thing:</p>
  
  <pre><code>b &lt;- function(data,name) { within(data,print(name)) }
  b(a,x)
  </code></pre>
  
  <p>However, this fails miserably:</p>
  
  <pre><code>Error in print(name) : object 'x' not found
  </code></pre>
  
  <p>What am I doing wrong? How does this work?</p>
  
  <p><strong>Note</strong>: this is <strong>not</strong> a duplicate of <a href="https://stackoverflow.com/questions/2948367/pass-variable-name-to-a-function-in-r">Pass variable name to a function in r</a></p>