title: do.call error "second argument must be a list" with S4Vectors when the code is in a library
body: <p>This is a question raised in the context of this other one: 
  <a href="https://stackoverflow.com/questions/40789601">Extract and paste together multiple columns of a data frame like object using a vector of column names</a></p>
  
  <p>I received an answer based on the usage of <code>do.call</code> which happens to not work when the code is part of a library, but worked well when implemented as part of the main script.</p>
  
  <p>Here is my attempt at making a simplified example:</p>
  
  <p>File <code>example.do.call.R</code>:</p>
  
  <pre><code>library(S4Vectors)
  library(test.package)
  data &lt;- DataFrame(A=letters[1:6], B=LETTERS[1:6])
  
  test_do_paste &lt;- function(data) {
      groups &lt;- c("A", "B")
      combined_letters &lt;- do.call(paste, data[groups])
      print(combined_letters)
  }
  
  test_do_paste(data)
  #test.package::test_do_paste(data)
  </code></pre>
  
  <p>The same <code>test_do_paste</code> function is present as a single function in a package I organize and install according to <a href="https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/" rel="nofollow noreferrer">https://hilaryparker.com/2014/04/29/writing-an-r-package-from-scratch/</a>.</p>
  
  <p>When running <code>Rscript example.do.call.R</code> as above, I obtain the expected result:</p>
  
  <pre><code>[1] "a A" "b B" "c C" "d D" "e E" "f F"
  </code></pre>
  
  <p>However, if I switch to the commented version (using the function from the library), I get the following error:</p>
  
  <pre><code>Error in do.call(paste, data[groups]) : second argument must be a list
  Calls: &lt;Anonymous&gt; -&gt; do.call
  Execution halted
  </code></pre>
  
  <p>My questions are:</p>
  
  <ol>
  <li><p><strong>Why is that?</strong></p></li>
  <li><p><strong>How can I avoid the error?</strong></p></li>
  </ol>
  
  <hr>
  
  <h2>Some investigation</h2>
  
  <h3>I used a normal <code>data.frame</code> instead of the <code>DataFrame</code></h3>
  
  <p>In this case, the error doesn't happen.</p>
  
  <p>Note that in my real application, I'm actually using some more complicated object than a DataFrame, but there is a <code>colData</code> function that extracts a <code>DataFrame</code> from this object.</p>
  
  <p>Maybe there would be a way to circumvent the problem if I was able to convert this further into a normal <code>data.frame</code>.</p>
  
  <p><code>as.data.frame</code> actually does this, so question 2 has at least this solution in my case.</p>
  
  <h3>I inserted a <code>print(class(data[groups]))</code> in the function</h3>
  
  <p>In both cases I get the following:</p>
  
  <pre><code>[1] "DataFrame"
  attr(,"package")
  [1] "S4Vectors"
  </code></pre>
  
  <p>So, apparently, even in the case that works, the object I give as second argument to <code>do.call</code> is not a list.</p>
  
  <h3>I inserted a <code>print(do.call)</code> in the function.</h3>
  
  <p>When run from the library, I obtain just a few lines:</p>
  
  <pre><code>function (what, args, quote = FALSE, envir = parent.frame())
  {
      if (!is.list(args)) 
          stop("second argument must be a list")
      if (quote) 
          args &lt;- lapply(args, enquote)
      .Internal(do.call(what, args, envir))
  }
  &lt;bytecode: 0x1b7cdd8&gt;
  &lt;environment: namespace:base&gt;
  </code></pre>
  
  <p>But when run in the script, there is much more output:</p>
  
  <pre><code>function (what, args, quote = FALSE, envir = parent.frame()) 
  standardGeneric("do.call")
  &lt;environment: 0x1d8c568&gt;
  attr(,"generic")
  [1] "do.call"
  attr(,"generic")attr(,"package")
  [1] "BiocGenerics"
  attr(,"package")
  [1] "BiocGenerics"
  attr(,"group")
  list()
  attr(,"valueClass")
  character(0)
  attr(,"signature")
  [1] "what" "args"
  attr(,"default")
  Method Definition (Class "derivedDefaultMethod"):
  
  function (what, args, quote = FALSE, envir = parent.frame()) 
  {
      if (!is.list(args)) 
          stop("second argument must be a list")
      if (quote) 
          args &lt;- lapply(args, enquote)
      .Internal(do.call(what, args, envir))
  }
  &lt;bytecode: 0x1d8e660&gt;
  &lt;environment: namespace:base&gt;
  
  Signatures:
          what 
  target  "ANY"
  defined "ANY"
  attr(,"skeleton")
  (function (what, args, quote = FALSE, envir = parent.frame()) 
  {
      if (!is.list(args)) 
          stop("second argument must be a list")
      if (quote) 
          args &lt;- lapply(args, enquote)
      .Internal(do.call(what, args, envir))
  })(what, args, quote, envir)
  attr(,"class")
  [1] "standardGeneric"
  attr(,"class")attr(,"package")
  [1] "methods"
  </code></pre>
  
  <p>This raises more questions:</p>
  
  <ol start="3">
  <li><p><strong>Why this difference?</strong></p></li>
  <li><p><strong>Could it explain the error in the "library" case?</strong></p></li>
  </ol>