why ellipsis ... have to be wrapped inside c() when used in lambda functions in purrr

Viewed 75

May be this question termed as part-3 of this question.

I know that arguments in lambda functions when used in tidyverse especially purrr functions are written as -

  • . when there is only 1 argument
  • .x & .y when there are 2
  • OR ..1, ..2 so on when there are > 2 arguments

In the linked question, I learnt that if all the arguments have to be simultaneously passed we may use ellipsis i.e. ....

But my question, why such ... work only when wrapped inside a c() and doesn't work when used as such. In the below two syntaxes, second one works while first one doesn't?

#1 this doesn't work

pmap_df(iris[1:4], ~...)

Error in .f(Sepal.Length = .l[[1L]][[i]], Sepal.Width = .l[[2L]][[i]],  : 
  '...' used in an incorrect context



#2 this however, works and returns the first argument after converting it to a tibble
pmap_df(iris[1:4], ~ c(...))

#see
identical(pmap_df(iris[1:4], ~c(...)), iris[1:4] %>% as_tibble())
[1] TRUE

Can someone explain?

1 Answers

The offical documentation has this to be said about the ellipsis:

The components of ‘...’ can be accessed in the usual pairlist manner from C code, but is not easily accessed as an object in interpreted code.

So that's possibly part of the answer to your question: The ellipsis is a not easily accessed object.

You would likely have to go on a safari trip down into the C-code of things to learn more about that.

Now being a not easily accessed object the ... certainly doesn't qualify as a list or vector, which is what pmap_df wants. Although technically it probably could.

However seeing how ~ is used to create one-liner function bodies, the real reason is probably that it itself does not consider the ellipsis in the first place.

Consider:


f <- function( ... ) {
    ~...
}

f(a=1,b=2) ## returns just `~...` , no trace of a and b in the formula's environment either.

Now consider: ~........ . Works just as fine.

~ happily takes any number of dots or other alfa num characters, ~....... or ~aaa......bb...cc... for example, effectively demonstrating it doesn't care about the otherwise normal way to look at the ellipsis and just treats them as part of a name.

Related