title: R - How to use a function varaible to refernce an object for a left join in R
body: <p>I'm trying to join two data sets in R using a left join in a function. Firstly I have my main data frame GE_GC referenced by the &quot;df&quot; in the function and I am trying to join a data frame called GE_GC_Teacher_Names however I would like the &quot;GE_GC&quot; part of the object name to be dynamic as I have multiple data sets with unique set of names that needs to be joined. For example if the &quot;df&quot; reference in my function was EX_EF then the function would join the EX_EF_Teacher_Names data frame onto the EX_EF data frame.</p>
  <pre><code>
  Q2_Table &lt;- function(df){
    
    
    df %&gt;% select(contains(&quot;Q2_&quot;)) %&gt;% 
    gather(var,value) %&gt;% 
    group_by(var) %&gt;%
    summarise(
      Mean = round(mean(as.numeric(value), na.rm = TRUE), 2),
      Responses = length(value[!is.na(value)]),
      &quot;Very Dissatisfied&quot; = paste0(length(value[value == &quot;1&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;1&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Dissatisfied&quot; = paste0(length(value[value == &quot;2&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;2&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Neutral&quot; = paste0(length(value[value == &quot;3&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;3&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Satisfied&quot; = paste0(length(value[value == &quot;4&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;4&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Very Satisfied&quot; = paste0(length(value[value == &quot;5&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;5&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;)
    ) %&gt;%
      left_join(
        as.name(paste0((deparse(substitute(df))),&quot;_Teacher_Names&quot;)), #Here works with imputed df name but trying to dynamically name teacher df
        by = 'var'
      ) %&gt;%
        rename(&quot;Teacher&quot; = teacher) %&gt;%
        select(-var, -value) %&gt;%
        relocate(Teacher, .before = Mean)
  }
  
  Q2_Output &lt;- Q2_Table(GE_GC)
  </code></pre>
  <p>When trying to run this function I get the following error even though a matching column called &quot;var&quot; is present in the GE_GC and GE_GC_Teacher_Names data frames.</p>
  <blockquote>
  <p>Error in <code>auto_copy()</code>:
  ! <code>x</code> and <code>y</code> must share the same src.
  i set <code>copy</code> = TRUE (may be slow).
  Run <code>rlang::last_error()</code> to see where the error occurred. &gt;</p>
  </blockquote>
  <p>The following code works fine when I input the teacher data frame name manually</p>
  <pre><code>Q2_Table &lt;- function(df){
    
    
    df %&gt;% select(contains(&quot;Q2_&quot;)) %&gt;% 
    gather(var,value) %&gt;% 
    group_by(var) %&gt;%
    summarise(
      Mean = round(mean(as.numeric(value), na.rm = TRUE), 2),
      Responses = length(value[!is.na(value)]),
      &quot;Very Dissatisfied&quot; = paste0(length(value[value == &quot;1&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;1&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Dissatisfied&quot; = paste0(length(value[value == &quot;2&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;2&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Neutral&quot; = paste0(length(value[value == &quot;3&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;3&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Satisfied&quot; = paste0(length(value[value == &quot;4&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;4&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;),
      &quot;Very Satisfied&quot; = paste0(length(value[value == &quot;5&quot; &amp; !is.na(value)]), &quot; (&quot;, sprintf(&quot;%1.0f%%&quot;, length(value[value == &quot;5&quot; &amp; !is.na(value)])/Responses*100), &quot;)&quot;)
    ) %&gt;%
      left_join(
        GE_GC_Teacher_Names, #Here works with imputed df name but trying to dynamically name teacher df
        by = 'var'
      ) %&gt;%
        rename(&quot;Teacher&quot; = teacher) %&gt;%
        select(-var, -value) %&gt;%
        relocate(Teacher, .before = Mean)
  }
  
  Q2_Output &lt;- Q2_Table(GE_GC)
  </code></pre>
  <p>So this section is the problem:</p>
  <pre><code>left_join(
        as.name(paste0((deparse(substitute(df))),&quot;_Teacher_Names&quot;)), #Here works with imputed df name but trying to dynamically name teacher df
        by = 'var'
      )
  </code></pre>
  <p>Any help would be appreciated thank you.</p>