title: Rename in dplyr 0.7+ function
body: <p>I am having difficulty renaming columns in a function using dplyr. I have already found helpful posts on nonstandard evaluation and the use of enquo (e.g., <a href="http://dplyr.tidyverse.org/articles/programming.html" rel="nofollow noreferrer">http://dplyr.tidyverse.org/articles/programming.html</a> and  <a href="https://stackoverflow.com/questions/45076971/changing-names-of-resulting-variables-in-custom-dplyr-function">Changing names of resulting variables in custom dplyr function</a>). The ultimate goal is to use the function to summarise each group, and then rename the columns into something more meaningful than the original variable names. </p>
  
  <p>Here is a simple example, which works except for the commented-out lines.</p>
  
  <pre><code>library(tidyverse)
  
  myfunc &lt;- function(df, groupvar, colvar, grouplab, collab) {
    groupvar &lt;- enquo(groupvar)
    colvar &lt;- enquo(colvar)
    grouplab &lt;- enquo(grouplab)  # quo_name instead?
    collab &lt;- enquo(collab)  # quo_name instead?
  
  t &lt;- df %&gt;%
    select(!!groupvar, !!colvar) %&gt;%
    group_by(!!groupvar, !!colvar) %&gt;%
    summarise(
      Frequencies = length(!!colvar),
      Percentages = length(!!colvar) / nrow(df)
      ) %&gt;%
    mutate(
      Frequencies = scales::comma(Frequencies),
      Percentages = scales::percent(Percentages)
      ) #%&gt;%
  # rename(
  #    (!!grouplab) = !!groupvar,  # add a more descriptive grouping var label
  #    (!!collab) = !!colvar)  # add a more descriptive column var label
  
  # Error: unexpected '=' in: rename((!!grouplab) = "
  
  ...  # code to create an xtable, print it into LaTeX
  }
  
  myfunc &lt;- (df = mtcars, groupvar = gear, colvar = cyl, grouplab = "Vehicle Gears", collab = "Vehicle Cylinders")
  # I purposely made grouplab and collab have a space in case that changes anything. 
  </code></pre>
  
  <p>Outside of functions, the relevant part would read simply: <code>mtcars &lt;- mtcars %&gt;% rename("Vehicle Cylinders" = cyl, "Vehicle Gears" = gear)</code></p>
  
  <p>I must be getting tripped up in how renaming operates in a function vs. outside of it. Any advice? I'm open to suggestions. </p>
  
  <p><strong>UPDATE</strong><br>
  Here is one way to do it. I'd rather not put the variable names in strings, but don't know a way around it here. Open to suggestions.</p>
  
  <pre><code>library(tidyverse)
  library(xtable)
  data(mtcars)
  
  t_groupedfreqprop &lt;- function(df, groupvar, colvar, grouplab, collab, plotname) {
  groupvar2 &lt;- rlang::sym(groupvar)
  colvar2 &lt;- rlang::sym(colvar)
  grouplab &lt;- rlang::sym(grouplab)
  collab &lt;- rlang::sym(collab)
  
  t &lt;- df %&gt;%
    select(!!groupvar2, !!colvar2) %&gt;%
    group_by(!!groupvar2, !!colvar2) %&gt;%
    summarise(
      Frequencies = length(!!colvar2),
      Percentages = length(!!colvar2) / nrow(df)
    ) %&gt;%
    mutate(
      Frequencies = scales::comma(Frequencies),
      Percentages = scales::percent(Percentages)
    ) %&gt;%
    rename(
      !!grouplab := !!rlang::sym(groupvar2),
      !!collab := !!rlang::sym(colvar2)
    )
  
  # remainder of function removed
  }
  
  t_groupedfreqprop(df = mtcars, groupvar = "gear", colvar = "cyl", grouplab = "Vehicle Gears", collab = "Vehicle Cylinders")
  </code></pre>