title: Prevent coersion to a single type in unlist() or c(); passing arguments to wrapper functions
body: <p>Is there a simple way to flatten a list while retaining the original types of list constituents?..  Is there a way to programmatically construct a flattened list out of different constituent types?..</p>
  
  <p>For instance, I want to create a simple wrapper for functions like <code>png(filename,width,height)</code> that would take device name, file name, and a list of options.  The naive approach would be something like</p>
  
  <pre><code>my.wrapper &lt;- function(dev,name,opts) { do.call(dev,c(filename=name,opts)) }
  </code></pre>
  
  <p>or similar code with <code>unlist(list(...))</code>.  This doesn't work because <code>opts</code> gets coerced to character, and the resulting call is e.g. <code>png(filename,width="500",height="500")</code>.</p>
  
  <p>If there's no straightforward way to create heterogeneous lists like that, is there a standard idiomatic way to splice arguments into functions without naming them explicitly (e.g. <code>do.call(dev,list(filename=name,width=opts["width"])</code>)?</p>
  
  <p>-- Edit --</p>
  
  <p>Gavin Simpson answered both questions below in his discussion about constructing wrapper functions.  Let me give a summary of the answer to the title question: </p>
  
  <p>It is possible to construct a list with <code>c()</code> provided at least one of the arguments to <code>c()</code> is a list.  To wit:</p>
  
  <pre><code>&gt; foo &lt;- c("a","b"); bar &lt;- 1:3
  &gt; c(foo,bar)
  [1] "a" "b" "1" "2" "3"
  &gt; typeof(c(foo,bar)) 
  [1] "character"  ## note that the result is not a list and that coercion occurred
  &gt; c(list(foo),list(bar)) ## try also c(foo,list(bar))
  [[1]]     [1] "a" "b"
  [[2]]     [1] 1 2 3
  &gt; typeof(c(foo,list(bar)))  
  [1] "list"       ## now the result is a list, so no need to coerce to same type
  &gt; c(as.list(foo),as.list(bar)) ## this creates a flattened list, as desired
  [[1]]     [1] "a"
  [[2]]     [1] "b"
  [[3]]     [1] 1
  [[4]]     [1] 2
  [[5]]     [1] 3
  </code></pre>