为什么我收到 "Object" not found within this function?

Why I'm receiving "Object" not found within this function?

query_averager <- function(arg1,
                           arg2) {
n = tibble()
   i = 1
   while (i <= 100) {
   n[i] <- cpquery(fitted = fitted_bn_01,
        event = (ret == "Acima da Selic") ,
        evidence = (solidez == arg1) & (resultado == arg2))
   i = i+1

   }
return (mean(n))
}

Query_result <- query_averager(arg1 = "1", arg2 = "Lucro acima da mediana")

这给了我“找不到对象 arg1”,但是当我在外部使用函数 cpquery() 时,它工作得很好。

没有 query_averager 函数的工作代码:

cpquery(fitted = fitted_bn_01,
    event = (ret == "Acima da Selic") ,
    evidence = (solidez == "1") & (resultado == "Lucro acima da median"))

我相信其他变量是什么并不重要,主要问题是为什么:

 solidez == "1"

有效,但下面的代码片段无效:

solidez == arg1

证据表达式中似乎没有任何变量,因为它们不会在函数体中求值。所以看起来你必须在评估之前操纵函数调用。我没有安装和运行这个包,但它似乎可以工作:

query_averager <- function(arg1, arg2) {
   n = tibble()
   i = 1
   while (i <= 100) {
   n[i] <- eval(substitute(cpquery(fitted = fitted_bn_01,
        event = (ret == "Acima da Selic") ,
        evidence = (solidez == arg1) & (resultado == arg2))))
   i = i+1

   }
  mean(n)
}

substitute() 部分将 arg1arg2 的值注入到表达式中。然后我们用 eval().

评估该表达式