title: pass only one argument to function from choice of a few and conditionally pipe within dplyr
body: <p>I'm looking for a way to <a href="https://stackoverflow.com/questions/30604107/r-conditional-evaluation-when-using-the-pipe-operator">conditionally</a> pass only one argument to a function (one of three choices). Based on the choice, I want to simply create a variable in the dataset. Lets say we have the following dataset:</p>
  
  <pre><code>set.seed(10)
  test &lt;- data.frame(time_stamp = sample(seq(as.Date('1999/01/01'), as.Date('2012/01/01'), by="day"), 12))
  test
  #    time_stamp
  # 1  2000-05-05
  # 2  2009-03-09
  # 3  2008-04-24
  # 4  2011-03-22
  # 5  2003-05-27
  # 6  2003-01-01
  # 7  2008-10-22
  # 8  2003-10-13
  # 9  2011-02-26
  # 10 2008-08-27
  # 11 2011-12-30
  # 12 2001-07-18
  </code></pre>
  
  <p>My desired output when I run my function is the following:</p>
  
  <pre><code>test_fun(type = "halfs") 
  #or more simply
  test_fun(halfs)
  #    time_stamp half_var
  # 1  2000-05-05  H1 2000
  # 2  2009-03-09  H1 2009
  # 3  2008-04-24  H1 2008
  # 4  2011-03-22  H1 2011
  # 5  2003-05-27  H1 2003
  # 6  2003-01-01  H1 2003
  # 7  2008-10-22  H2 2008
  # 8  2003-10-13  H2 2003
  # 9  2011-02-26  H1 2011
  # 10 2008-08-27  H2 2008
  # 11 2011-12-30  H2 2011
  # 12 2001-07-18  H2 2001
  </code></pre>
  
  <p>Based on the argument chosen I run an <code>if</code> statement within a pipe, I thought I could do this if I put {} around the conditional statement as mentioned <a href="https://stackoverflow.com/questions/30604107/r-conditional-evaluation-when-using-the-pipe-operator">here</a>, but I can't figure it out. Heres the function:</p>
  
  <pre><code>    test_fun &lt;- function(type = c("halfs", "quarts", "other")) {
        test %&gt;% {
          if (type == "halfs") {
            mutate(half_var = ifelse(month(time_stamp) &lt;= 6, paste('H1', year(time_stamp)), paste('H2', year(time_stamp))))
          }  else if (type == "quarts") {
            mutate(quarts_var = case_when(month(time_stamp) &lt;= 3 ~ paste('q1', year(time_stamp)), 
                                          month(time_stamp) &gt; 3 &amp; month(time_stamp) &lt;= 6 ~ paste('q2', year(time_stamp)),
                                          month(time_stamp) &gt; 6 &amp; month(time_stamp) &lt;= 9 ~ paste('q3', year(time_stamp)),
                                          month(time_stamp) &gt; 9 ~ paste('q4', year(time_stamp))))
          }  else (type == "other") {
            mutate(other = ifelse(month(time_stamp) &lt;= 6, paste('H1', year(time_stamp)), paste('H2', year(time_stamp))))
          }
        }
  
  }
  </code></pre>
  
  <p>I'm getting an error about unexpected brackets but I think the problem is to do with conditional if within a pipe (all brackets are closed).</p>
  
  <p>Another approach might be using optional argument as suggested <a href="https://stackoverflow.com/questions/28370249/correct-way-to-specifiy-optional-arguments-in-r-functions">here</a>  <code>test_fun &lt;- function(halfs, quarts = NULL, other = NULL))</code> but that way indicates that <code>halfs</code> must be supplied which is not the case. Really I want something like <code>test_fun &lt;- function(halfs = NULL, quarts = NULL, other = NULL))</code> or <code>test_fun &lt;- function(...))</code> which cant be <a href="https://stackoverflow.com/questions/50511127/pass-optional-arguments-to-function-three-dots">done</a>. A way around that might be to supply the data as an argument: <code>test_fun &lt;- function(test, halfs = NULL, quarts = NULL, other = NULL))</code> but I cant figure it out. </p>
  
  <p>Any suggestions would be great.</p>