call(NAME, ...) is.call(x) as.call(x)
is.call checks whether x is a function call
as.call tries to coerce x to a function call.
One use of call is in functions that create other functions. The d.order example below is a non-trivial example; d.order is a function that creates the density function of an order statistic from a sample from a specific distribution.
funs <- c("sin", "cos")
my.call <- call(funs[which], as.name("x"))
# my.call ready to be evaluated or put into an expression
eval (my.call)
# function that creates the density function of an order statistic
d.order <- function(n, r, distfun, densityfun)
{
f <- function(x)
NULL
con <- exp(lgamma(n + 1) - lgamma(r) - lgamma(n - r + 1))
c1 <- call(substitute(distfun), as.name("x"))
c2 <- call("^", call("-", 1, c1), n - r)
c3 <- call("^", c1, r - 1)
c4 <- call("*", con, call("*", c2, call("*", c3, call(substitute(
densityfun), as.name("x")))))
f[[length(f)]] <- c4
f
}
d.median.norm.9 <- d.order(9, 5, pnorm, dnorm)
d.median.norm.9(-2:2)