3

I have a larger section of code but I've narrowed down the problem to this - So I want to return a concatenated list.

do.call(c,"X")
Error in do.call(c, "X") : second argument must be a list

So above it complains about the SECOND argument not being a list.

asimplelist=list(2,3,4)
class(asimplelist)
[1] "list"
do.call(c,asimplelist)
Error in do.call(c, asimplelist) : 
'what' must be a function or character string

Why will this not return a concatenated list ? C is a legit function, and it's being passed a list?

args(do.call)
function (what, args, quote = FALSE, envir = parent.frame()) 
NULL 

So "what" is the function argument it is complaining about.

Alessandro Jacopson
  • 16,221
  • 13
  • 91
  • 139
Doug C
  • 33
  • 5

1 Answers1

4

I will answer "stealing" my answer from this comment by Nick Kennedy:

It might be better to put the c in double quotes.

If the user has a non-function named c in the global environment, do.call(c, dates) will fail with the error "Error in do.call(c, list(1:3)) : 'what' must be a character string or a function".

Clearly it may not be best practice to define c, but it's quite common for people to do a <- 1; b <- 2; c <- 3.

For most purposes, R still works fine in this scenario; c(1, 2) will still work, but do.call(c, x) won't.

Of course if the user has redefined c to be a function (e.g. c <- sum), then do.call will use the redefined function.

Alessandro Jacopson
  • 16,221
  • 13
  • 91
  • 139