r - 这个 R 成语的目的(match.call 后跟 eval(parent.frame())
问题描述
我继承了一些使用我认为是库的常见 R 习惯用法的代码,但我不确定以这种冗长的方式编写会实现什么。最终我打算重写,但在我做一些愚蠢的事情之前,我首先想知道为什么。
ecd <-
function(formula, data, subset, weights, offset, ...) {
cl = match.call()
mf = match.call(expand.dots = FALSE)
m =
match(c("formula", "data", "subset", "weights", "offset"),
names(mf),
0L)
mf = mf[c(1L, m)]
mf$drop.unused.levels = TRUE
mf[[1L]] = quote(stats::model.frame)
mf = eval(mf, parent.frame())
mt = attr(mf, "terms")
y = stats::model.response(mf, "numeric")
w = as.vector(stats::model.weights(mf))
offset = as.vector(stats::model.offset(mf))
x = stats::model.matrix(mt, mf, contrasts)
z = ecd.fit(x, y, w, offset, ...)
我目前的理解是它从函数的原始参数构造一个函数调用对象(类型?)然后手动调用它,而不是直接调用stats::model.frame
。任何见解将不胜感激。
解决方案
我认为这应该回答一切,解释在代码中:
# 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
推荐阅读
- ol3-google-maps - 在 Openlayers 4 中使用 ol3-google-maps
- apache-kafka - 如果 ID=0 的代理关闭,Kafka 集群将无法工作
- algorithm - 给定一个单词字典和一个数组字母,找到可以使用这些字母创建的字典单词的最大数量
- python - “注入”有什么作用?
- graph - 使用 AQL 进行图形查询
- html - Openrefine 无法在手风琴内获取 html 代码
- tensorflow - 损失函数可调用(以函数的形式)是 TensorFlow 最佳实践吗?除了 Eager Execution 兼容性之外的其他优势?
- c++ - for(;i<=m;i++) 是什么意思?
- reactjs - redux-form 验证没有得到任何值
- graphics - 什么是“有效的 IM 颜色”?