Description Usage Arguments Details Value Author(s) See Also Examples
This is an extended version of the bquote
utility. 'qq' quotes its first argument, then scans for
terms wrapped in .()
, ..()
, or names that match
`.()`
The wrapped expressions or names are evaluated in the
given environment. Expressions wrapped in ..()
will be
interpolated into the argument list in which they occur. Names
wrapped in '.()' will be substituted and coerced to name.
1 |
expr |
An expression, left unevaluated. |
Invocations of qq() can be nested within the .()
sections and they should work as promised.
For qq
, A language object; for qe
, evaluates
the expression in the calling environment.
Peter Meilstrup
macro bquote substitute quoting.env
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #Basic substitution
default <- 1
qq( function(x, y = .(default)) x+y )
#function(x, y = 1) x + y
# splicing substitution:
paste.before <- alist("hello", "cool")
paste.after <- alist("!", "Now is", date())
qq(cat(...(paste.before), "world", ...(paste.after), '\n'))
#cat("hello", "cool", "world", "!", "Now is", date(), "\n")
# Name substitution:
element_to_access <- "x"
qq(function(data) data$`.(element_to_access)`)
#function(data) data$x
argument.name <- "x"
qq(
function(`.(argument.name)`)
cat(.(argument.name), " is ", `.(argument.name)`, "\n")
)
#function(x) cat("x", " is ", x, "\n"))
# Note that in the argument list to a function, function argument
# names are names, and defaults are values; that is
function(x=1, y) x+y
# is equivalent to
function(.=...(alist(x=1, y=))) x+y
# or
function(.=...(list(x=1, y=missing_value()))) x+y
# Building a function with an arbitrary list of arguments:
argnames <- letters[1:4]
qq(function(.=...(put(missing_value(length(argnames)), names, argnames))) {
list(...(lapply(argnames, as.name)))
})
#function(a, b, c, d) list(a, b, c, d)
# The poor .() function is overloaded. Usually can escape it by adding
# parens:
dfname <- "baseball"
qq(ddply(.(as.name(dfname)), (.)(id, team), identity))
#ddply(baseball, .(id.team), identity)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.