#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)
Run the code above in your browser using DataLab