1

Many R packages allow functions that take expressions as arguments. Some, however, go a step further. For example, the plyr package by @hadley boldly defines a function named .:

> .
function (..., .env = parent.frame()) 
{
    structure(as.list(match.call()[-1]), env = .env, class = "quoted")
}
<environment: namespace:plyr>

In my environment, ?'.' produces "No help found for topic in any package." On the surface, it looks like .() provides a mechanism for delayed evaluation that automatically captures the surrounding environment:

> x <- c(1,2,3)
> dot <- .(x + 10)
> dot
List of 1
 $ x + 10: language x + 10
 - attr(*, "env")=<environment: R_GlobalEnv> 
 - attr(*, "class")= chr "quoted"
> dot[[1]]
x + 10
> eval(dot[[1]])
[1] 11 12 13

Is that all that's going on? I understand the purpose of the env attribute but why is class = "quoted" important?

With all the different mechanisms R has to define expressions as well as delay and force evaluation, what are some of the benefits and costs of using the pattern of .() when passing expressions?

Sim
  • 13,147
  • 9
  • 66
  • 95
  • 2
    Seems that the answers are in the `Details` section in `?'.'` – Matthew Lundberg Dec 23 '12 at 03:19
  • 1
    @MatthewLundberg I get "No help found for topic in any package" when I do this, which is why I posted this question. Since I could see help for other `plyr` functions, I presumed that this one was undocumented but, having consulted the PDF doc for the package, it all makes sense. Since others may experience this doc problem in R, if you post your suggestion or quote the doc as an answer I will accept it. – Sim Dec 23 '12 at 04:26

1 Answers1

1

From ?'.'

Details

Similar tricks can be performed with substitute, but when functions can be called in multiple ways it becomes increasingly tricky to ensure that the values are extracted from the correct frame. Substitute tricks also make it difficult to program against the functions that use them, while the quoted class provides as.quoted.character to convert strings to the appropriate data structure.

Matthew Lundberg
  • 42,009
  • 6
  • 90
  • 112