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?