title: Using strings as variable names in cor.test in R function
body: <p>How to use a string as a variable name inside <code>cor.test</code> function in R? I am trying to write a general function that will allow running some correlation tests.</p>
  <pre><code>library(tidyverse)
  data(economics)
  
  crtest  &lt;- function(valy, valx){
      attach(economics)
      rlnshp  &lt;- cor.test(!!sym(valy), !!sym(valx))
      detach(economics)
  }
  crtest(&quot;uempmed&quot;, &quot;pce&quot;)
  </code></pre>
  <p>This gives an error message:</p>
  <pre><code>Error in !sym(valy) : invalid argument type
  </code></pre>
  <p>Is there a way to avoid using the <code>attach()</code> command? And get some way to generate <code>economics$pce</code>. <code>paste0</code> did not work as the string generated does not convert to the variable name in the text.</p>
  <p>Thanks for your patience.</p>