如何确保我的非标准评估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我在这里使用“不寻常”的不科学意义上的“非标准”。我知道什么是非标准的,但这是另一个(而且过于宽泛?)的问题。