0

I'm trying to understand ... and/or (...) in R. I understand that somehow this is used for entering unknown or multiple parameters to a function, but when is this necessary and/or useful? I've searched rdocumentation for it, but found nothing. In the R Language Definition it is defined but in very abstract terms.

Hence I ask: why is ... useful? Is it just not sloppy coding? Wouldn't it be better to pass arguments explicitly?

histelheim
  • 4,938
  • 6
  • 33
  • 63
  • 2
    useful when passing arguments through to some other argument-rich function (e.g. `plot`) – Ben Bolker Feb 08 '15 at 18:33
  • @rawr no, that's not the same question. This question asks "why" not "how." – Matt Ball Feb 08 '15 at 18:49
  • 4
    the titles may be different, but if one were to *actually read* it, then one would find that they are indeed duplicates. If you want to be technical, asking *why* something is useful is an opinion question and should be closed anyway. Take your pick mr ball – rawr Feb 08 '15 at 18:52
  • You may find answers to [this question](http://stackoverflow.com/questions/28370249/correct-way-to-specifiy-optional-arguments-in-r-functions/28370943#28370943) useful – Jthorpe Feb 08 '15 at 19:13
  • 1
    You obviously have an opinion on this design issue, and it happens to be different than the opinion of most R users. Invitations to flamewars should be closed. – IRTFM Feb 08 '15 at 19:19
  • A bat man reference seems appropriate in response to BondedDust's comment: https://www.youtube.com/watch?v=FNt0anp7WK8 – Tyler Rinker Feb 08 '15 at 19:35
  • @BondedDust - are you referring to the original question or one of the comments? If it is the former, I'm interested to know what the "opinion on this design issue" is, because I have no idea - I'm just trying to learn. – histelheim Feb 08 '15 at 19:37
  • 1
    @histelheim Take a function that actually takes another function as an argument such as `lapply` here it is not possible to supply all the parameters because it's contingent upon what function is passed as an argument. Also it makes the documentation less verbose and easier to parse when the argument list the similar for multiple functions. – Tyler Rinker Feb 08 '15 at 19:43

2 Answers2

2

It's called varargs (short for variadic arguments).

when is this necessary?

It's not strictly necessary, and it's not inherently sloppy. The "non-sloppy" alternative to varargs in any language is to pass an array or list as a single variable. So varargs is just syntactic sugar on top of a collection of things.

[when is it] useful?

Any time you want to save a few keystrokes and implicitly construct a list when calling a function.

Wouldn't it be better to pass arguments explicitly?

Depends. What are your criteria?

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
1

The ellipsis gives you the possibility to define functions with an unknown number of arguments/parameters.

It necessary for functions like c or list where the number of arguments given by the user is unknown.

If you type c or list in the R console you'll see that both of these functions use ... as arguments.

In these two cases it would be hard to pass arguments explicitly since the user can pass as many arguments as needed.

You can look at this post for more examples

Community
  • 1
  • 1
NicE
  • 21,165
  • 3
  • 51
  • 68
  • This doesn't answer the question - most of what you wrote is already stated in the question. When is this useful? Can you give an example of when you cannot pass the arguments explicitly? – histelheim Feb 08 '15 at 18:32
  • I dit it in the post, like for the function `c`, when you create a vector of size 5 like c(1,2,3,4,5), you pass 5 arguments, of size 4 like c(1,2,3,4), you pass 4 arguments so the only way to write that function is to use the ellipsis which allows you to pass as many arguments as needed – NicE Feb 08 '15 at 18:35