23

I recently saw a function in R where someone had used . as an argument. I can't seem to find any documentation on this (other than the use of ellipsis or "dot-dot-dot"). Can someone either point me in the direction of documentation or provide an example of usage?

hello.world <- function(.) "Hello World"
# function(.) is what I'm asking about.
Brandon Bertelsen
  • 43,807
  • 34
  • 160
  • 255
  • 1
    this came up on cross validated as well, some other good tidbits over there: http://stats.stackexchange.com/questions/10712/what-is-the-meaning-of-the-dot-in-r/10739#10739 – Chase Mar 11 '12 at 20:17

3 Answers3

17

Dot is a a valid character in symbol names just like any letter, so . is no different than let's say a - it has no special meaning in this context. You can write things like:

> . <- 10
> . + .
[1] 20

It may look strange but is valid in R. The above use function(.) is let's say unusual, but syntactically valid. Since the author did not reference . in the function body, we will never know if he meant ... or just used it because he could.

Simon Urbanek
  • 13,842
  • 45
  • 45
  • 1
    It does have a slightly special meaning at the start of identifiers: http://cran.r-project.org/doc/manuals/R-lang.html#Identifiers – huon Mar 11 '12 at 06:36
  • Not really. There are several conventions in R that make use of dots (hiding symbols, S3 dispatch, ...), but `.` as asked above has no special meaning. – Simon Urbanek Mar 11 '12 at 06:38
  • it has a special meaning for `proto`: ["formal argument lists of methods must always have a first argument of dot (i.e. .) which signifies the object on which the method is operating. The dot refers to the current object in the same way that a dot refers to the current directory in UNIX"](http://cran.r-project.org/web/packages/proto/vignettes/proto.pdf) – baptiste Mar 11 '12 at 06:43
  • 2
    @baptiste It is really just a convention that the author of `proto` prefers, you can in fact use any other name with no difference. – Simon Urbanek Mar 11 '12 at 06:49
  • There is also some mention of it in the See Also section of `?"function"` with respect to return(.) but the sentence doesn't make much sense to me: `debug for debugging; using invisible inside return(.) for returning invisibly` – Brandon Bertelsen Mar 11 '12 at 07:22
  • @SimonUrbanek indeed, you're right. I was misled by that statement in the vignette. – baptiste Mar 11 '12 at 08:08
  • `.` also has several special meanings in formulas ("all the other variables", "the previous left-hand side", "the previous right-hand side": see `?as.formula` and `?update.formula`) and it is a function in the `plyr` package. – Vincent Zoonekynd Mar 11 '12 at 10:21
  • 1
    @baptiste, Thanks for pointing out that quote regarding dot and proto. In fact, the quote is wrong or at least misleading and a FAQ related to it has now been added to the proto home page: http://code.google.com/p/r-proto/#FAQ – G. Grothendieck Mar 11 '12 at 13:20
3

While the answer given by Simon Urbanek is correct here's a reason the . is preferred over other characters as a function argument. In some situations, like lapply a function needs to receive an argument by design. But if there is no need of the argument inside the function we still need a dummy argument name. A . is the smallest character - almost invisible, so you are not distracted by what the function is delivering. The function(.) is as good as function() but has an advantage that it will not give an error when used in apply family.

Lazarus Thurston
  • 1,197
  • 15
  • 33
0

The dot notation is frequently used in the map family for outer parameters. For instance, in map() function of the purrr package the .x parameter is a list or vector, and the .f parameter is a function of formula:

map(.x, .f, ...)

If we have a list dat_list we cannot make a reference to the dat_list inside the formula, as dat_list is not in the formula environment:

library(purrr)
map(dat_list, ~lm(var1 ~ var2, data = dat_list) )
Error in eval(predvars, data, env)

But we can reference the outer parameter inside the formula as .x:

map(dat_list, ~lm(var1 ~ var2, data = .x) )
Freeman
  • 5,810
  • 3
  • 47
  • 48