3

如何确保我的非标准评估1使用data.table从父框架继承它需要的变量?

根据我对动态范围的理解,我下面的代码应该可以工作,但不能。我究竟做错了什么?

细节

我有许多函数的列表,我想将它们应用于data.table返回布尔检查和消息的单个函数(当检查为 TRUE 时)。例如,假设我正在审计一个帐户表。

library(data.table)
#----- Example data -----------------------------------------------------------
n <- 100
set.seed(123)
df <- data.table( acct_id      = paste0('ID',seq(n)),
                  acct_balance = round(pmax(rnorm(n,1000,5000),0)),
                  days_overdue = round(pmax(rnorm(n,20,20),0))
                  )
#----- Example list of rules to check (real case has more elements)------------
AuditRules <- list(
  list(
    msg_id = 1,
    msg_cat = 'Balance',
    cond_fn = function(d) d[, acct_balance > balance_limit ],
    msg_txt = 
      function(d) d[, paste('Account',acct_id,'balance is',
                            acct_balance - balance_limit, 
                            'over the limit.')]
  ),
  list(
    msg_id = 2,
    msg_cat = 'Overdue',
    cond_fn = function(d) d[, days_overdue > grace_period ],
    msg_txt = 
      function(d) d[, paste('Account',acct_id,'is overdue',
                            days_overdue-grace_period,
                            'days beyond grace period.')]
  )
)

我正在遍历规则列表并检查每个规则的数据集。

期望的输出

这在全球环境中运行良好。

balance_limit <- 1e4
grace_period  <-  14
audit <- rbindlist(
              lapply(AuditRules, function(item){
                with( item,
                      df[ cond_fn(df),
                         .(msg_id, 
                           msg_cat,
                           msg_txt = msg_txt(.SD) )
                         ]
                      )
                } )
            )
print(head(audit), row.names=FALSE)
#-----------------   Result   --------------------------------------
# msg_id msg_cat                                             msg_txt
#      1 Balance        Account ID44 balance is 1845 over the limit.
#      1 Balance        Account ID70 balance is 1250 over the limit.
#      1 Balance        Account ID97 balance is 1937 over the limit.
#      2 Overdue Account ID2 is overdue 11 days beyond grace period.
#      2 Overdue  Account ID3 is overdue 1 days beyond grace period.
#      2 Overdue  Account ID6 is overdue 5 days beyond grace period.

什么不起作用(需要解决方案)

rm(balance_limit, grace_period) # see "aside"

auditTheData <- function(d, balance_limit = 1e4, grace_period=14){
  rbindlist(
    lapply(AuditRules, function(item){
        with( item,
              d[ cond_fn(d),
                  .(msg_id, 
                    msg_cat,
                    msg_txt = msg_txt(.SD) )
                  ]
        )
    } )
  )
}
auditTheData(df)

导致错误:

Error in eval(jsub, SDenv, parent.frame()) : 
  object 'balance_limit' not found

这不是问题with(),尽管我已经阅读 ( ?with),通常应该避免将其用于编程。这也不起作用:

auditTheData2 <- function(d, balance_limit = 1e4, grace_period=14){
  rbindlist(
    lapply(AuditRules, function(item){
          d[ item[['cond_fn']](d),
             .(msg_id, 
               msg_cat,
               msg_txt = item[['msg_txt']](.SD) )
             ]
    } )
  )
}
auditTheData2(df) # Same error

顺便说一句:rm(balance_limit, grace_period)如果你在“什么不起作用”功能之前不做——即将它们留在全局环境中——你会得到想要的结果。因此,似乎function(item)-edlapply可以“看到”全局环境,但不能“看到”父环境(AuditTheData)。


1我在这里使用“不寻常”的不科学意义上的“非标准”。我知道什么是非标准的,但这是另一个(而且过于宽泛?)的问题。

4

1 回答 1

3

这似乎有效:

ar <- list(
  list(
    cat = 'Balance',
    cond_expr = quote(acct_balance > balance_limit),
    msg_expr = quote(sprintf('Account %s balance is %s over the limit.',
      acct_id, 
      acct_balance - balance_limit))
  ),
  list(
    cat = 'Overdue',
    cond_expr = quote(days_overdue > grace_period),
    msg_expr = quote(sprintf('Account %s is overdue %s days beyond grace period.', 
      acct_id, 
      days_overdue-grace_period))
  )
)

audDT = rbindlist(rapply(ar, list, "call", how = "replace"), id="msg_id")

auditem = function(d, a, balance_limit = 1e4, grace_period = 14){
    a[, {
        cond    = cond_expr[[1]]
        msg     = msg_expr[[1]]
        .(txt = d[eval(cond), eval(msg)])
    }, by=.(msg_id, cat)]
}

例如 ...

> head(auditem(df, audDT))
   msg_id     cat                                                 txt
1:      1 Balance        Account ID44 balance is 1845 over the limit.
2:      1 Balance        Account ID70 balance is 1250 over the limit.
3:      1 Balance        Account ID97 balance is 1937 over the limit.
4:      2 Overdue Account ID2 is overdue 11 days beyond grace period.
5:      2 Overdue  Account ID3 is overdue 1 days beyond grace period.
6:      2 Overdue  Account ID6 is overdue 5 days beyond grace period.

我不确定这些更改中的哪些有所不同:

  • eval预定义的表达式,而不是将它们组合在j函数内部
  • 使用表格作为规则,有一些好处:
    • 由于每个条目都应该具有相同的结构,因此您可以验证每个条目的格式是否正确(没有缺少的组件)
    • msg_id可以自动编号,rbindlist因此不必手动输入
    • by=可以用来代替lapply,因为后者有一些奇怪的评估行为

我也切换paste到了,sprintf但我确信这没关系。

rapply是必要的,因为 data.table 不支持调用/表达式作为列类型(显然),但支持列表列。

于 2018-02-16T23:52:29.670 回答