title: Dplyr function with optional default argument and required ellipiss
body: <p>I've a simple function that adds counts for unique combination of variables:</p>
  
  <h1>Function</h1>
  
  <pre><code># Add tally summary for group
  add_tally &lt;- function(df, n = "n", ...) {
    # Grpup variables
    group_vars &lt;- rlang::quos(...)
  
    # Check if ellipsis is empty
    if (length(group_vars) == 0) {
      stop("Missing grouping variables")
    }
  
    none &lt;- Negate(any)
  
    # Check that passed object is data frame or tibble
    if (none(tibble::is_tibble(df), is.data.frame(df))) {
      stop("Passed object should be a data frame or tibble.")
    }
  
    if (hasArg("n")) {
      # Take varname
      varname &lt;- n
    } else {
      varname &lt;- "n"
    }
  
    df %&gt;%
      group_by(!!!group_vars, add = TRUE) %&gt;%
      mutate(!!varname := sum(n())) %&gt;%
      ungroup()
  
  }
  </code></pre>
  
  <h2>Example</h2>
  
  <p>It's fairly straightforward: </p>
  
  <pre><code>&gt;&gt; mtcars[,c("am", "gear")] %&gt;% add_tally(n = "my_n", am,gear)
  # A tibble: 32 x 3
        am  gear  my_n
     &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;
   1  1.00  4.00     8
   2  1.00  4.00     8
   3  1.00  4.00     8
   4  0     3.00    15
   5  0     3.00    15
   6  0     3.00    15
   7  0     3.00    15
   8  0     4.00     4
   9  0     4.00     4
  10  0     4.00     4
  </code></pre>
  
  <h1>Problem</h1>
  
  <p>I would like for the <strong><code>n</code></strong> argument to be optional. I.e. if not explicitly defined <em>(as <code>my_n</code> in the example above),</em> I would like for the argument to take default <strong><em><code>n</code></em></strong> value. As it would usually happen with <em><code>n = "n"</code>,</em> which is now redundant due to attempted <code>hasArgs()</code> call.</p>
  
  <h2>Example</h2>
  
  <p>This fails:</p>
  
  <pre><code>&gt;&gt; mtcars[,c("am", "gear")] %&gt;% add_tally(am,gear)
  Error in add_tally(., am, gear) : object 'am' not found
  </code></pre>
  
  <h3>Desired results</h3>
  
  <pre><code># A tibble: 32 x 3
            am  gear  n
         &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;
       1  1.00  4.00     8
       2  1.00  4.00     8
       3  1.00  4.00     8
       4  0     3.00    15
       5  0     3.00    15
       6  0     3.00    15
       7  0     3.00    15
       8  0     4.00     4
       9  0     4.00     4
      10  0     4.00     4
  </code></pre>