按值

时间:2016-06-10 13:52:19

标签: r

我正在一个函数中构建模型,类似于:

some_form <- formula("Species == 'virginica' ~  Petal.Width")
some_fam <- "binomial"
some_fit <- glm(formula = some_form, family = some_fam, data = iris )

当我在结果上调用summary时,函数调用包含变量名。

summary(some_fit) # could also just use some_fit$call
# Returns
# Call:
#   glm(formula = some_form, family = some_fam, data = iris)

但我希望让它返回原始(评估)的公式和姓氏。我想要的输出如下:

# To produce desired output
summary(glm(Species=='virginica' ~ Petal.Width, family = binomial, data = iris))
# Call:
#   glm(formula = Species == "virginica" ~ Petal.Width, family = binomial, data = iris)
#   ^ This is what I want!

我尝试过:

some_fit <- glm(eval(some_form), family = eval(some_fam), data = iris )
summary(some_fit)
# Returns
# Call:
#   glm(formula = eval(some_form), family = eval(some_fam), data = iris)

eval()eval(quote())替换eval(substitute())我得到了类似的结果 - $call只是复制我传入其中的任何表达式。

这可能是一个基本问题(并且无论如何,如果它是重复的,请标记 - 我可能没有使用正确的搜索条件),但我真诚地感谢:

  1. 如何将这些参数作为值传递的基本解释(而不仅仅是引用,如果这就是我目前正在做的事情)
  2. 此方法存在任何陷阱,尤其是在函数内执行时。

0 个答案:

没有答案