title: collect only if query returns less than n_max rows
body: <p>Occasionally when connecting to my <code>Oracle</code> database through <code>ROracle</code> and <code>dbplyr</code> I will run a <code>dplyr::collect</code> operation that fetches more data than expected and than R can handle.</p>
  
  <p>This can make R crash and is often a sign I should have filtered or aggregated data further before fetching.</p>
  
  <p>It would be great to be able to check the size of the result before choosing to fetch it or not (without running the query twice).</p>
  
  <p>Let's name <code>collect2</code> the variation of <code>collect</code> that would allow this:</p>
  
  <p>expected behavior:</p>
  
  <pre><code>small_t &lt;- con %&gt;% tbl("small_table") %&gt;%
    filter_group_etc %&gt;%
    collect2(n_max = 5e6) # works fine
  
  big_t   &lt;- con %&gt;% tbl("big_table")   %&gt;%
    filter_group_etc %&gt;%
    collect2(n_max = 5e6) # Error: query returned 15.486.245 rows, n_max set to 5.000.000
  </code></pre>
  
  <p>Would this be possible ?</p>
  
  <p>I'm also open to a solution using <code>ROracle</code> / <code>DBI</code> without <code>dplyr</code>, e.g.:</p>
  
  <pre><code>dbGetQuery2(con, my_big_sql_query,n_max = 5e6) # Error: query returned 15.486.245 rows, n_max set to 5.000.000
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>See below a partial solution posted as an answer, not optimal because some time is wasted fetching data I have no use for.</p>