首页 > 解决方案 > 这个 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。任何见解将不胜感激。

标签: r

解决方案


我认为这应该回答一切,解释在代码中:

# 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

推荐阅读