title: R: IF statement in dplyr::filter requires ELSE otherwise fails?
body: <p>Per <a href="https://stackoverflow.com/questions/47624161/use-filter-in-dplyr-conditional-on-an-if-statement-in-r">other answers</a>, one can include <code>if</code> statements in pipes and within <code>dplyr</code> functions. However for <code>filter</code>, it seems like one needs to use an <code>else</code> rather than just an <code>if</code>, otherwise the <code>filter</code> will return nothing, calling the error:</p>
  <blockquote>
  <p>no applicable method for 'filter_' applied to an object of class
  &quot;NULL&quot;</p>
  </blockquote>
  <p>Which is rather misleading IMHO since I'm not using <code>filter_</code> and am not personally feeding the pipe a NULL class object. Min reprex:</p>
  <pre><code>data(mtcars)
  a &lt;- 1
  mtcars %&gt;% filter(if (a == 1) cyl == 6) # if in filter works
  a &lt;- NA
  mtcars %&gt;% filter(if (is.na(a)) cyl == 6) # is.na in if in filter works if it evaluates to true
  a &lt;- 4
  mtcars %&gt;% filter(if (!is.na(a)) cyl == a) # !is.na works if it evaluates to true
  a &lt;- NA
  mtcars %&gt;% filter(if (!is.na(a)) cyl == a) # doesn't work if it evaluates to false
  # Input `..1` must be of size 32 or 1, not size 0.
  </code></pre>
  <p>One can use else to generate an output from the filter, by selecting a filter constraint that will include all results.</p>
  <pre><code>mtcars %&gt;% filter(if (!is.na(a)) cyl == a else carb &gt; 0
  </code></pre>
  <p>There's probably a better catch-all filter constraint, and indeed probably a more elegant way of doing this in general, but I couldn't find anyone else having found this problem so if nothing else, hopefully my progress serves its <a href="https://cdn.shopify.com/s/files/1/0535/6917/products/mistakesdemotivator.jpeg?v=1554328460" rel="nofollow noreferrer">usual purpose</a>.</p>
  <p>Any intel as to whether this is a bug, or other ways to approach this, most welcome. Thanks.</p>