title: How can column names by used in a complex dplyr function that uses base R elements?
body: <p>First, this question is likely a repeat, however the solutions I've tried (eg using <code>enquo()</code> and <code>!!</code>, or <code>get()</code>, or <code>{{}}</code>) have not yielded results.</p>
  <p><strong>The Problem</strong></p>
  <p>I have a function that needs to take column names passed to it in a pipeline, perform a series of dplyr-based functions with some base R components, and return the new dataframe. The problem is that the function will not take the column names passed to it as variables in the referenced dataframe, treating them as strings instead.</p>
  <p><strong>The Data</strong></p>
  <p>This code will create a usable dataframe for this problem:</p>
  <pre><code>df_ext &lt;- tibble(ID = c(rep(1, 5), rep(2, 5)),
                   TIME = rep(c(1, 2, 3, 4, 5), 2),
                   VAL = c(0, 1, 2, 2, 3, 1, 5, 0, 1, 4))
  </code></pre>
  <p><strong>The Current Function</strong></p>
  <p>Here's a version of the function that I can share. It's designed to create a series of categories for the data I pass to it, but this is a simplified version that just calculates some basic groupings (ie, it doesn't do much of anything).</p>
  <pre><code>my_fun &lt;- function(.data, id, time, val){
    
    require(&quot;dplyr&quot;)
    
    df &lt;- df |&gt;
      group_by(id) |&gt;
      mutate(val_lag = if_else(val &gt; 0, time - lag(time, default = 0), 0)) |&gt;
      mutate(first_time = min(time),
             last_time = max(time),
             first_val_pos = ifelse(any(val), min(time[val &gt; 0]), NA),
             last_val_pos = ifelse(any(val &gt; 0), max(time[val &gt; 0]), NA)) |&gt;
      group_by(grp = cumsum(val_lag == 0)) |&gt;
      mutate(val_pos_run = cumsum(val_lag)) |&gt;
      ungroup() |&gt;
      group_by(id) |&gt;
      mutate(ada_bl = ifelse(first_val_pos &lt;= 0, val[time == first_val_pos], 0)) |&gt;
      ungroup()
    
    df
    
    
  }
  
  df_ext |&gt;
    my_fun(id = ID, time = TIME, val = VAL)
  </code></pre>
  <p>If anyone can get the columns from the dataframe to pass into the function and get treated as columns in the pipe-referenced dataframe, you'd be ending a very frustrating headache.</p>