title: Specifying multiple variables to group by via explicit argument with unquoted elements
body: <p>Based on the section regarding <em>capturing multiple arguments</em> in <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html" rel="nofollow noreferrer">Programming with dplyr</a>, I am trying to specify</p>
  
  <ol>
  <li><p>multiple variables to group by in <code>dplyr::group_by</code></p></li>
  <li><p>without relying on <code>...</code> but using an explicit list argument <code>group_vars</code> instead</p></li>
  <li><p>without needing to quote the list elements in arg <code>group_vars</code></p></li>
  </ol>
  
  <p>Example data</p>
  
  <pre><code>df &lt;- tibble::tribble(
    ~a,   ~b,  ~c,
    "A",  "a", 10,
    "A",  "a", 20,
    "A",  "b", 1000,
    "B",  "a", 5,
    "B",  "b", 1
  )
  </code></pre>
  
  <p>Approach based on <code>...</code> from <a href="https://cran.r-project.org/web/packages/dplyr/vignettes/programming.html" rel="nofollow noreferrer">Programming with dplyr</a></p>
  
  <pre><code># Approach 1 -----
  my_summarise &lt;- function(df, ...) {
    group_vars &lt;- dplyr::enquos(...)
  
    df %&gt;%
      dplyr::group_by(!!!group_vars) %&gt;%
      dplyr::summarise(x = mean(c))
  }
  
  my_summarise(df, a, b)
  #&gt; # A tibble: 4 x 3
  #&gt; # Groups:   a [2]
  #&gt;   a     b         x
  #&gt;   &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A     a        15
  #&gt; 2 A     b      1000
  #&gt; 3 B     a         5
  #&gt; 4 B     b         1
  </code></pre>
  
  <p>Approach based on list argument with quoted elements:</p>
  
  <pre><code># Approach 2 -----
  my_summarise_2 &lt;- function(df, group_vars = c("a", "b")) {
    group_vars &lt;- dplyr::syms(group_vars)
  
    df %&gt;%
      dplyr::group_by(!!!group_vars) %&gt;%
      dplyr::summarise(x = mean(c))
  }
  
  my_summarise_2(df)
  #&gt; # A tibble: 4 x 3
  #&gt; # Groups:   a [2]
  #&gt;   a     b         x
  #&gt;   &lt;chr&gt; &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A     a        15
  #&gt; 2 A     b      1000
  #&gt; 3 B     a         5
  #&gt; 4 B     b         1
  
  my_summarise_2(df, group_vars = "a")
  #&gt; # A tibble: 2 x 2
  #&gt;   a         x
  #&gt;   &lt;chr&gt; &lt;dbl&gt;
  #&gt; 1 A      343.
  #&gt; 2 B        3
  </code></pre>
  
  <p>I can't find an approach that lets me supply unquoted column names:</p>
  
  <pre><code># Approach 3 -----
  my_summarise_3 &lt;- function(df, group_vars = list(a, b)) {
    group_vars &lt;- dplyr::enquos(group_vars)
  
    df %&gt;%
      dplyr::group_by(!!!group_vars) %&gt;%
      dplyr::summarise(x = mean(c))
  }
  
  my_summarise_3(df)
  #&gt; Error: Column `list(a, b)` must be length 5 (the number of rows) or one, not 2
  </code></pre>
  
  <p>I guess the crucial thing is to end up with an identical list structure as the
  one after calling <code>group_vars &lt;- dplyr::enquos(...)</code>:</p>
  
  <pre><code>&lt;list_of&lt;quosure&gt;&gt;
  
  [[1]]
  &lt;quosure&gt;
  expr: ^a
  env:  global
  
  [[2]]
  &lt;quosure&gt;
  expr: ^b
  env:  global
  </code></pre>
  
  <p>I tried to tackle it with <code>group_vars %&gt;% purrr::map(dplyr::enquo)</code>, but of course R complains about <code>a</code> and <code>b</code> as they need to be evaluated.</p>