title: Breaking while loop containing lists
body: <p>I have a function that iterates polynomial expression on a predictor up until the linear model output shows that predictor without significance. This is all done in a <code>while(TRUE)</code> loop because I want to iterate all the degrees of a polynomial (unlikely I would actually need anything greater than 5), however I cannot get the loop to break properly. It should break when the length of the <code>lists</code> are 0 via <code>compact</code>.</p>
  <p>For example:</p>
  <pre><code>regression_polynomial &lt;- function(data, response_chosen, predictor_chosen){
    pmod &lt;- c()
    pmod_I &lt;- c() 
    i=0
    while(TRUE){
      i = i+1
      form &lt;- reformulate(glue('poly({predictor_chosen},{i}, raw=TRUE)'), response=response_chosen)
       pmod[[i]] &lt;- lm(form, data) %&gt;% summary() %&gt;%
        .$coefficients %&gt;% data.frame(.) %&gt;% rownames_to_column() %&gt;% .[!(.$rowname) %in% &quot;(Intercept)&quot;,] %&gt;% 
       `colnames&lt;-`(c('predictor', 'Estimate', 'Std.Error', 't-value', 'Pr(&gt;|t|)')) %&gt;% 
         remove_rownames() %&gt;% split(., sort(as.numeric(rownames(.)))) %&gt;% 
         lapply(., function(x){if(x$`Pr(&gt;|t|)` &lt; 0.05)x } ) %&gt;% 
         compact() 
       length_pmod &lt;- sapply(pmod, length)
       if(length(length_pmod) == 0){
         break
       }
  
  
    }
    return(pmod)
  }
  library(faraway)
  regression_polynomial(savings, 'sr', 'pop15')
  &gt;Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  NA/NaN/Inf in 'x'
  </code></pre>