title: dplyr: NSE in joins (by)
body: <p>I had a hard time to figure out how I could join two tables using dplyr::left_join with NSE. The problem was that I could not supply the right value to 'by'. I think I have found a solution for now, but it feels like I am doing it in an extra complicated way. So, if you know an easier/more elegant solution, please let me know :)</p>
  
  <p>That's what I am doing: </p>
  
  <pre><code># some data
  df &lt;- dplyr::tibble(x=1:10,
                      y=LETTERS[1:10],
                      z=LETTERS[11:20])
  
  # some function
  test_fun &lt;- function(df,id){
    id &lt;- rlang::enquo(id)
    join_var &lt;- names(rlang::quos_auto_name(id))[2] # is there an easier way?
    # not important: doing some stuff
    df1 &lt;- df %&gt;%
      dplyr::select(!!id,y)
    df2 &lt;- df %&gt;%
      dplyr::select(!!id,z)
    # join using prepared join_var
    df1 %&gt;%
      dplyr::left_join(df2,setNames(join_var,join_var))
  }
  
  test_fun(df,id=x)
  </code></pre>
  
  <p>As I was asked for additional context and some more explaination to better understand my problem, I will try to give some more information here:</p>
  
  <p>NSE is an abbreviation for non-standard evaluation. My goal was to build a function that joins to datasets based on a variable the user could define. The main problem was that the by-argument in dplyr::left_join did not accept the quoted variable (NSE argument) and so I had to find a workaround. The way I found (join_var &lt;- names(rlang::quos_auto_name(id))[2]) seemd a little complicated and I was looking for an easier solution. That was - by the way - provided by Lionel Henry ... thanks :)</p>