Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top