title: Functions and non-standard evaluation in dplyr
body: <p>I just finished reading <a href="https://dplyr.tidyverse.org/articles/programming.html" rel="noreferrer">'Programming with dplyr'</a> and <a href="https://ggplot2.tidyverse.org/reference/aes_.html" rel="noreferrer">'Define aesthetic mappings programatically'</a> to start to get a grip on non-standard evaluation of functions. The specific question for this post is, <strong>"How do I write the code directly below using the tidyverse (eg <em><code>quo()</code></em>, <em><code>!!</code></em>, etc.)"</strong> instead of the base-R approach <code>eval()</code>, <code>substitute</code>, <code>etc.</code>.</p>
  
  <pre><code>library(tidyverse)
  xy &lt;- data.frame(xvar = 1:10, yvar = 11:20)
  
  plotfunc &lt;- function(data, x, y){
    y.sqr &lt;- (eval(substitute(y), envir = data))^2
    print(
      ggplot(data, aes_q(x = substitute(x), y = substitute(y.sqr))) + 
        geom_line()
    )
  }
  
  plotfunc(xy, xvar, yvar)
  </code></pre>
  
  <p><strong>Can you provide the answer?</strong> It would be a bonus if you could work in the following concept, being, why is the function above <em>non-standard</em> whereas this other function below is standard? I read the <em>Advanced R</em> chapters on functions and non-standard evaluation, but it's above my head at this point. Can you explain in layperson terms? The function below is clear and concise (to me) whereas the function above is a hazy mess.</p>
  
  <pre><code>rescale01 &lt;- function(x) {
    rng &lt;- range(x, na.rm = TRUE)
    (x - rng[1]) / (rng[2] - rng[1])
  }
  rescale01(c(0, 5, 10))
  </code></pre>