26

This may be a lame question, but anyways. In RStudio, I just noticed that typing a number, .. , and another number will change the syntax highlight of the characters from navy blue to baby blue for the .. and the numbers after it.

As an example, this is a number that has this distinctive color:

4..4

The part "..4" has the baby blue color.

I am using the default syntax coloring. I tried in the interpreter to introduce such constant but I only got the error "Error: unexpected numeric constant in "4..5", and the queries with "two dots" or .. does not seem to be very google friendly.

Does anyone knows what is the usage of ".." is, if any?

papirrin
  • 2,004
  • 22
  • 30
  • When I try this I get the highlighting just for ..4, I don't need a preceding number to trigger the highlight (and even when the preceding number is there, it doesn't get highlighted, only the ..4 part does). I'm using RStudio version 0.97.551 with R 3.0.1 – Tumbledown Nov 15 '13 at 09:13
  • Same here, I will update the answer to specify this properly. Thanks for checking! – papirrin Nov 15 '13 at 09:15

3 Answers3

31

..4 would be a reserved word in R's parser. Under ?Reserved you will find

... and ..1, ..2 etc, which are used to refer to arguments passed down from a calling function.

Example

#  Function will return nth element from ... ( n MUST be a named argument)
f <- function( ... , n = NULL )
   return( eval( parse( text = paste0( ".." , n ) ) ) )

#  Return third element of ...
f( n = 3 , 1:3 , 3:1 , 10:15 )
#[1] 10 11 12 13 14 15

#  Try to return element that is out of bounds
f( n = 4 , 1:3 , 3:1 , 10:15 )
#Error in eval(expr, envir, enclos) : 
#  the ... list does not contain 4 elements

Now that you know what it is, how do you use it? Courtesy of John Chambers;

"The name ..1 refers to the first matching argument, ..2 to the second, etc. You should probably avoid this obscure convention, which can usually be done by writing a function with some ordinary argument names, and calling it with "...""

Software for Data Analysis: Programming with R, John M. Chambers, Springer-Verlag, New York, 2008.
Excerpt from page 457.

Community
  • 1
  • 1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 2
    I think I'm glad I *didn't* know this -- I'm more afraid of what could go wrong when calling for , e.g., `..4` , than I am of Cthulhu! – Carl Witthoft Nov 15 '13 at 12:37
  • @CarlWitthoft John Chambers tends [**to agree**](http://chat.stackoverflow.com/transcript/message/12963266#12963266) with you! – Simon O'Hanlon Nov 15 '13 at 12:38
15

..n refers to the nth element in ....

Here's a slightly simpler alternative to Simon's answer, avoiding eval/parse.

f <- function(...)
{
  message("dots = ")
  print(list(...))   # Notice that you need to convert ...
                     # to a list or otherwise evaluate it
  message("..1 = ")
  print(..1)
  message("..2 = ")
  print(..2)
}

f(runif(5), letters[1:10])
## dots = 
## [[1]]
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## 
## [[2]]
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## 
## ..1 = 
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## ..2 = 
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • 1
    @SimonO101 OK, point taken. I've updated the comment in my code. – Richie Cotton Nov 15 '13 at 13:29
  • Note that putting variables in a list deep-copies them in memory in the current R implementation (which is a massive factor in R's memory wastefulness, since standard functions routinely do this, such as mapply). So if you are expecting huge arguments, you might want to access them through `match.call(expand.dots=TRUE)`. I could post an answer returning them in an `environment` without copying, if it's not getting too far from the original question. – codeola Mar 03 '14 at 04:34
  • @codeola I'm not sure that adds much to the "What is ..4?" question, but it sounds interesting. You could ask and answer a question about "How do I make `lapply`-type code more memory efficient" (or similar) if you want to elaborate. – Richie Cotton Mar 03 '14 at 11:21
8

The accepted answers make sense.

But I saw two dots in a completely different context (i.e. not inside a function), like this: ..prop..:

ggplot(data=diamonds) + 
  geom_bar(
   mapping=aes(x=cut, y=..prop.., group=1) 
  )

Turns out ..prop.. are special variables created by ggplot's stat_count transformation.

stat_count provides two internal variables ..count.. and ..prop.., referring to count and proportion respectively. Don’t be surprised by the ..name.. notation, it is used to prevent confusion with your own columns (don’t name your own columns with weird names like ..count..!)

(Remember variable names in R can include periods. I come from Python background, so this double-period seems like Python's double-underscore convention: __prop__, a technique used to mark special / "private" variables / "name mangle" those variables)

Nate Anderson
  • 18,334
  • 18
  • 100
  • 135