Why is match.call useful?
One reason that is relevant here is that match.call
captures the language of the call without evaluating it, and in this case it allows lm
to treat some of the "missing" variables as "optional". Consider:
lm(x ~ y, data.frame(x=1:10, y=runif(10)))
Vs:
lm2 <- function (
formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...
) {
mf <- model.frame(
formula = formula, data = data, subset = subset, weights = weights
)
}
lm2(x ~ y, data.frame(x=1:10, y=runif(10)))
## Error in model.frame.default(formula = formula, data = data, subset = subset, :
## invalid type (closure) for variable '(weights)'
In lm2
, since weights
is "missing" but you still use it in weights=weights
, R tries to use the stats::weights
function which is clearly not what was intended. You could get around this by testing for missingness before you call model.frame
, but at that point the match.call
starts looking pretty good. Look at what happens if we debug
the call:
debug(lm2)
lm2(x ~ y, data.frame(x=1:10, y=runif(10)))
## debugging in: lm2(x ~ y, data.frame(x = 1:10, y = runif(10)))
## debug at #5: {
## mf <- model.frame(formula = formula, data = data, subset = subset,
## weights = weights)
## }
Browse[2]> match.call()
## lm2(formula = x ~ y, data = data.frame(x = 1:10, y = runif(10)))
match.call
doesn't involve the missing arguments at all.
You could argue that the optional arguments should have been made explicitly optional via default values, but that's not what happened here.
Purpose of this R idiom (match.call followed by eval(parent.frame())
I think this should answer everything, explanations are in the code :
# for later
FOO <- function(x) 1000 * x
y <- 1
foo <- function(...) {
cl = match.call()
message("cl")
print(cl)
message("as.list(cl)")
print(as.list(cl))
message("class(cl)")
print(class(cl))
# we can modify the call is if it were a list
cl[[1]] <- quote(FOO)
message("modified call")
print(cl)
y <- 2
# now I want to call it, if I call it here or in the parent.frame might
# give a different output
message("evaluate it locally")
print(eval(cl))
message("evaluate it in the parent environment")
print(eval(cl, parent.frame()))
message("eval.parent is equivalent and more idiomatic")
print(eval.parent(cl))
invisible(NULL)
}
foo(y)
# cl
# foo(y)
# as.list(cl)
# [[1]]
# foo
#
# [[2]]
# y
#
# class(cl)
# [1] "call"
# modified call
# FOO(y)
# evaluate it locally
# [1] 2000
# evaluate it in the parent environment
# [1] 1000
# eval.parent is equivalent and more idiomatic
# [1] 1000
match.call() returns a function or a symbol, but symbols can't be used by do.call()
As you’ve noticed, do.call
constructs and invokes a call expression (and invokes it) from a function name (or function) and a list of arguments. But you already have a call expression, no need for do.call
— so you can directly invoke it using eval
or eval.parent
:
recursive_function_call = function(call) {
call$drop_options = NULL
eval.parent(call)
}
recursive_function_call(match.call())
That said, I’m not sure what your function’s purpose is. Are you looking for Recall
?
Alternatively, you can use as.call
to coerce a list into a function call expression. And, once again, eval
then evaluates it:
eval.parent(as.call(fun_cal))
— in fact, as.call
is essentially the inverse of as.list
on a call expression: identical(as.call(as.list(call)), call)
is TRUE
.
`match.call()` and `sys.call()` called from a function of the enclosing environment
Just to give you a different point of view of the problem itself,
you could just save the call in the enclosing environment,
always matching it in the "main" function:
factory <- function(){
matched_call <- NULL
CALL <- function(){
print(matched_call)
}
CALL2 <- function() {
CALL()
}
function(x, y){
matched_call <<- match.call()
on.exit(matched_call <<- NULL)
...
}
}
match.call with default arguments
Hopefully, this doesn't lead to dragons.
foo <- function(x=NULL,y=NULL,z=2) {
mget(names(formals()),sys.frame(sys.nframe()))
}
foo(x=4)
$x
[1] 4
$y
NULL
$z
[1] 2
print(foo(x=4))
$x
[1] 4
$y
NULL
$z
[1] 2
Related Topics
Is It Bad Practice to Access S4 Objects Slots Directly Using @
Stl Decomposition of Time Series with Missing Values for Anomaly Detection
Writing Functions in R, Keeping Scoping in Mind
Add Text to Geom_Line in Ggplot
How to Extract Everything Until First Occurrence of Pattern
Ggplot2: Define Plot Layout with Grid.Arrange() as Argument of Do.Call()
Defining Minimum Point Size in Ggplot2 - Geom_Point
Include Tikz Code in Bookdown Figure Environment
R - Common Title and Legend for Combined Plots
Insert Portions of a Markdown Document Inside Another Markdown Document Using Knitr
Dplyr - Mean for Multiple Columns
"Long Vectors Not Supported Yet" Error in Rmd But Not in R Script