title: R: specifying variable name in function parameter for a function of general (universal) use
body: <p>Here is my small function and data. Please note that I want to design a function not personal use for general use.   </p>
  
  <pre><code>dataf &lt;- data.frame (A= 1:10, B= 21:30, C= 51:60, D = 71:80)
  
  myfun &lt;- function (dataframe, varA, varB) {
                daf2 &lt;- data.frame (A = dataframe$A*dataframe$B, 
                B= dataframe$C*dataframe$D)
                anv1 &lt;- lm(varA ~ varB, daf2)
                print(anova(anv1)) 
               }             
  
  myfun (dataframe = dataf, varA = A, varB = B)
  
  Error in eval(expr, envir, enclos) : object 'A' not found
  </code></pre>
  
  <p>It works with when I specify data$variable name, but I do not want to make such specification so that it requires the user to write both data and variable name in the function. </p>
  
  <pre><code> myfun (dataframe = dataf, varA = dataf$A, varB = dataf$B)
  Analysis of Variance Table
  
  Response: varA
            Df Sum Sq Mean Sq    F value    Pr(&gt;F)    
  varB       1   82.5    82.5 1.3568e+33 &lt; 2.2e-16 ***
  Residuals  8    0.0     0.0                         
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
  Warning message:
  In anova.lm(anv1) :
    ANOVA F-tests on an essentially perfect fit are unreliable
  </code></pre>
  
  <p>what is best practice in this situation? can I put attach the dataframe inside the function? what could be disadvantages or potential conflicts / dangers on doing so? See the masked statement in output. I believe once it is attached will remain attached reminder of session right? THe function provided here is just example, I need more downstream analysis where names of variables from different dataframe can be / should be identical. I am expecting a programmer solution on this. </p>
  
  <pre><code>myfun &lt;- function (dataframe, varA, varB) {
                attach(dataframe)
                   daf2 &lt;- data.frame (A = A*B, B= C*D)
                anv1 &lt;- lm(varA ~ varB, daf2)
                return(anova(anv1))
               }             
  
  myfun (dataframe = dataf, varA = A, varB = B)
  
  The following object(s) are masked from 'dataframe (position 3)':
  
      A, B, C, D
  Analysis of Variance Table
  
  Response: varA
            Df Sum Sq Mean Sq    F value    Pr(&gt;F)    
  varB       1   82.5    82.5 1.3568e+33 &lt; 2.2e-16 ***
  Residuals  8    0.0     0.0                         
  ---
  Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
  Warning message:
  In anova.lm(anv1) :
    ANOVA F-tests on an essentially perfect fit are unreliable
  </code></pre>