title: Flatten nested list into 1-deep list
body: <p>I am looking for an efficient solution to (recursively) flatten a nested list (of arbitrary depth) into non-nested, 1 deep list. The list elements are not homogeneous, therefore they should not be unlisted into a vector (that would coerce all values to a single type). The best solution so far is:</p>
  
  <pre><code>flatlist &lt;- function(mylist){
      lapply(rapply(mylist, enquote, how="unlist"), eval)
  }
  </code></pre>
  
  <p>This does almost what I want:</p>
  
  <pre><code>&gt; flatlist(list(foo=TRUE, bar=456, pets=list(cat="meeuw", dog="woof")))
  $foo
  [1] TRUE
  
  $bar
  [1] 456
  
  $pets.cat
  [1] "meeuw"
  
  $pets.dog
  [1] "woof"
  </code></pre>
  
  <p>However, a problem is that <code>rapply</code> is dropping <code>NULL</code> values, which is undesired:</p>
  
  <pre><code>&gt; flatlist(list(foo=123, bar=NULL))
  $foo
  [1] 123
  </code></pre>
  
  <p>I would like that <code>NULL</code> elements appear in the output, either as <code>NULL</code> or as <code>NA</code>. Also the double loop with <code>enquote</code> and then <code>eval</code> makes things a bit slow. This function is used extensively in my code. Is there a way to do it all in one run?</p>