title: Passing variables into the names_glue parameter of tidyr::pivot_wider
body: <p>Here's some silly data that we pivot wider, using two names:</p>
  <pre><code>library(tidyr)
  
  df &lt;- data.frame(
      food = c('banana','banana','banana','banana','cheese','cheese','cheese','cheese'),
      binary = c(rep(c('yes','no'), 4)),
      car = c('toyota','subaru','mazda','skoda','toyota','subaru','mazda','skoda'),
      fun = c(2,4,3,6,2,4,2,3))
  
  df %&gt;%
      pivot_wider(
          id_cols = food,
          names_from = c(car, binary),
          values_from = fun)
  </code></pre>
  <p>If we want to change the format of the new variable names, e.g., from <code>toyota_yes</code> to <code>yes_toyota</code>, we use the <code>names_glue</code> argument:</p>
  <pre><code>df %&gt;%
      pivot_wider(
          id_cols = food,
          names_from = c(car, binary),
          names_glue = &quot;{binary}_{car}&quot;,
          values_from = fun)
  </code></pre>
  <p>The issue I'm facing is finding the correct syntax to pass variable names to the <code>names_glue</code> parameter. It's easy to pass variables to <code>names_from</code>, e.g.:</p>
  <pre><code>var1 &lt;- 'car'
  var2 &lt;- 'binary'
  df %&gt;%
      pivot_wider(
          id_cols = food,
          names_from = c(var1, var2),
          values_from = fun)
  </code></pre>
  <p>But we can't directly do that with <code>names_glue</code>:</p>
  <pre><code>df %&gt;%
      pivot_wider(
          id_cols = food,
          names_from = c(var1, var2),
          names_glue = &quot;{var1}_{var2}&quot;,
          values_from = fun)
  </code></pre>
  <blockquote>
  <p>Error: Column names <code>car_binary</code>, <code>car_binary</code>, and <code>car_binary</code> must not be duplicated.</p>
  </blockquote>
  <p>Presumably it's evaluating the variable and just passing the resulting string (i.e., 'car' or 'binary') into the glue function. I have played around with some things I would normally use for tidy evaluation (<code>!!sym(...)</code>, etc.) but haven't got anything to work. Desired output is the following, using variables for the <code>names_glue</code> parameter:</p>
  <pre><code># A tibble: 2 x 5
    food   yes_toyota no_subaru yes_mazda no_skoda
    &lt;fct&gt;       &lt;dbl&gt;     &lt;dbl&gt;     &lt;dbl&gt;    &lt;dbl&gt;
  1 banana          2         4         3        6
  2 cheese          2         4         2        3
  </code></pre>
  <p><a href="https://github.com/tidyverse/tidyr/commit/e74748fca8737f7c632b22a8a703bc1779261e17" rel="nofollow noreferrer">Here is the commit that added the names_glue parameter to pivot_wider -- although I can't really figure out how to troubleshoot with this.</a></p>