检查父函数中是否缺少参数
Check for missing argument in parent function
问题
我有一个函数 f
可以计算调用它的环境的摘要。在这个简单的示例中,它只是对找到的所有对象求和。
f <- function(){
x <- ls(parent.frame())
sum(sapply(x, get, envir=parent.frame()))
}
g <- function(x = 7, y){
z <- 3
f()
}
但是,如果从缺少参数的函数中调用,则会引发错误。
R> g(y = 34)
[1] 44
R> g()
Error in FUN(c("x", "y", "z")[[2L]], ...) :
argument "y" is missing, with no default
为了适当地处理它,我需要一种方法来判断,从 f
内部,如果 y
或 g
环境中的其他任意对象是g
在这种情况下,如果它丢失了。
我目前的尝试
尝试不同的解决方案
debug(f)
g()
当然 missing(y)
不起作用,因为 y
不是 f
的参数。更改评估 missing
的环境也不起作用,因为我们仍处于调用堆栈的同一级别:
Browse[2]> eval(missing(y), parent.frame())
Error in missing(y) : 'missing' can only be used for arguments
Browse[2]> identical(sys.frames(), eval(sys.frames(), parent.frame()))
[1] TRUE
我能做的是使用肮脏的 hack
确定 y
是否是 g
函数的参数
Browse[2]> eval(substitute(missing(a), list(a="x")), parent.frame())
[1] TRUE
Browse[2]> eval(substitute(missing(a), list(a="y")), parent.frame())
[1] TRUE
Browse[2]> eval(substitute(missing(a), list(a="z")), parent.frame())
[1] FALSE
对两个参数 x
和 y
产生 TRUE
但不是普通变量 z
。将它与检查参数是否可以检索的 tryCatch
结合起来可以解决问题,但它非常脏:
is.argument <- eval(substitute(missing(a), list(a="y")), parent.frame())
if(is.argument){
tryCatch({
get("y", parent.frame())
FALSE
}, error = function(e) TRUE)
} else {
NA
}
此外,我无法弄清楚如何为任意参数定义 is.argument
,而不是上面示例中明确说明的 "y"
。
更新:目的
实际上,f
的目的是在运行时调试 g
。我可能会打电话
R> debug(g)
R> g()
单步执行它并使用 f
检查对象的状态,或者我可能会设置 options(error=recover)
并发现自己调试 g
如果它产生错误。在这两种情况下都应该有一个明确定义的调用堆栈,所以我想我的根本问题是它是否可以在不同级别上以类似于帧堆栈的方式查询(使用 sys.frames()
访问)。我必须承认,虽然这对我来说是深水区。
将 f
视为我自己的 ls.str
的调整版本,可以像这样使用:
Browse[2]> ls.str() # Inside g()
x : num 7
y : <missing>
在 ls.str
和 utils:::print.ls_str
中进行一些挖掘后,我发现它通过
完成了相同的任务
for (nam in x) {
cat(nam, ": ")
o <- tryCatch(get(nam, envir = E, mode = M), error = function(e) e)
if (inherits(o, "error")) {
cat(if (length(grep("missing|not found", o$message)))
"<missing>"
else o$message, "\n", sep = "")
} else {
strO <- function(...) str(o, ...)
do.call(strO, strargs, quote = is.call(o) || is.symbol(o))
}
}
除非有合适的方法来做到这一点,否则我将进行类似的修改。
缺失参数的值在与环境关联的配对列表中由称为 "empty symbol" 的奇数对象表示。事实证明,至少目前 "empty symbol" 也会通过调用 quote(expr=)
返回。 (See here 对空符号的一次讨论。)
函数 ls_safe()
使用这两个事实来实施替代的缺失测试。它 returns 由其 pos
参数指定的环境中存在的非缺失变量组成的字符向量。
ls_safe <- function(pos=1) {
## Capture the parent environment's frame as a list
ll <- as.list(parent.frame(pos))
## Check for "missing" variables
ii <- sapply(ll, function(X) identical(X, quote(expr=)))
names(ll)[!ii]
}
## Then just use ls_safe() in place of ls()
f <- function(){
x <- ls_safe(pos=2)
sum(sapply(x, get, envir=parent.frame()))
}
g <- function(x = 7, y){
z <- 3
f()
}
g(99)
## [1] 102
g(99, 1000)
## [1] 1102
我遇到了同样的问题:想要一个函数来检查参数是否丢失。我还首先尝试了基于 eval
的想法,即使缺少变量,它也总是给我 FALSE
。
Josh 在上面提供了一个解决方案,但它太具体了。我想要的是一个简洁的检查器函数,我可以将其添加到我的函数中,以便它们检查必要变量中的缺失并抛出信息性错误。这是我的解决方案:
check_missing = function(var_names, error_msg = "[VAR] was missing! Please supply the input and try again.") {
#parent.frame as list
pf = as.list(parent.frame())
#check each if missing
for (name in var_names) {
#is it there at all?
if (!name %in% names(pf)) {
stop(name + " is not even found in the parent.frame! Check the variable names!", call. = F)
}
#check if missing
if (are_equal(pf[[name]], quote(expr = ))) {
stop(str_replace(error_msg, pattern = "\[VAR\]", name), call. = F)
}
}
#all fine
return(invisible(NULL))
}
要测试它,使用这个:
test_func = function(y) {
check_missing("y")
print("OK")
}
测试一下:
test_func(y = )
# Error: y was missing! Please supply the input and try again.
# Called from: check_missing("y")
test_func(y = "k")
# [1] "OK"
剩下不满意的地方就是报错了错误的"Called from"信息。它 returns 检查器函数本身,但如果它返回父函数,它会提供更多信息。不知道能不能解决。
希望这可能对某人有用。
问题
我有一个函数 f
可以计算调用它的环境的摘要。在这个简单的示例中,它只是对找到的所有对象求和。
f <- function(){
x <- ls(parent.frame())
sum(sapply(x, get, envir=parent.frame()))
}
g <- function(x = 7, y){
z <- 3
f()
}
但是,如果从缺少参数的函数中调用,则会引发错误。
R> g(y = 34)
[1] 44
R> g()
Error in FUN(c("x", "y", "z")[[2L]], ...) :
argument "y" is missing, with no default
为了适当地处理它,我需要一种方法来判断,从 f
内部,如果 y
或 g
环境中的其他任意对象是g
在这种情况下,如果它丢失了。
我目前的尝试
尝试不同的解决方案
debug(f)
g()
当然 missing(y)
不起作用,因为 y
不是 f
的参数。更改评估 missing
的环境也不起作用,因为我们仍处于调用堆栈的同一级别:
Browse[2]> eval(missing(y), parent.frame())
Error in missing(y) : 'missing' can only be used for arguments
Browse[2]> identical(sys.frames(), eval(sys.frames(), parent.frame()))
[1] TRUE
我能做的是使用肮脏的 hack
确定y
是否是 g
函数的参数
Browse[2]> eval(substitute(missing(a), list(a="x")), parent.frame())
[1] TRUE
Browse[2]> eval(substitute(missing(a), list(a="y")), parent.frame())
[1] TRUE
Browse[2]> eval(substitute(missing(a), list(a="z")), parent.frame())
[1] FALSE
对两个参数 x
和 y
产生 TRUE
但不是普通变量 z
。将它与检查参数是否可以检索的 tryCatch
结合起来可以解决问题,但它非常脏:
is.argument <- eval(substitute(missing(a), list(a="y")), parent.frame())
if(is.argument){
tryCatch({
get("y", parent.frame())
FALSE
}, error = function(e) TRUE)
} else {
NA
}
此外,我无法弄清楚如何为任意参数定义 is.argument
,而不是上面示例中明确说明的 "y"
。
更新:目的
实际上,f
的目的是在运行时调试 g
。我可能会打电话
R> debug(g)
R> g()
单步执行它并使用 f
检查对象的状态,或者我可能会设置 options(error=recover)
并发现自己调试 g
如果它产生错误。在这两种情况下都应该有一个明确定义的调用堆栈,所以我想我的根本问题是它是否可以在不同级别上以类似于帧堆栈的方式查询(使用 sys.frames()
访问)。我必须承认,虽然这对我来说是深水区。
将 f
视为我自己的 ls.str
的调整版本,可以像这样使用:
Browse[2]> ls.str() # Inside g()
x : num 7
y : <missing>
在 ls.str
和 utils:::print.ls_str
中进行一些挖掘后,我发现它通过
for (nam in x) {
cat(nam, ": ")
o <- tryCatch(get(nam, envir = E, mode = M), error = function(e) e)
if (inherits(o, "error")) {
cat(if (length(grep("missing|not found", o$message)))
"<missing>"
else o$message, "\n", sep = "")
} else {
strO <- function(...) str(o, ...)
do.call(strO, strargs, quote = is.call(o) || is.symbol(o))
}
}
除非有合适的方法来做到这一点,否则我将进行类似的修改。
缺失参数的值在与环境关联的配对列表中由称为 "empty symbol" 的奇数对象表示。事实证明,至少目前 "empty symbol" 也会通过调用 quote(expr=)
返回。 (See here 对空符号的一次讨论。)
函数 ls_safe()
使用这两个事实来实施替代的缺失测试。它 returns 由其 pos
参数指定的环境中存在的非缺失变量组成的字符向量。
ls_safe <- function(pos=1) {
## Capture the parent environment's frame as a list
ll <- as.list(parent.frame(pos))
## Check for "missing" variables
ii <- sapply(ll, function(X) identical(X, quote(expr=)))
names(ll)[!ii]
}
## Then just use ls_safe() in place of ls()
f <- function(){
x <- ls_safe(pos=2)
sum(sapply(x, get, envir=parent.frame()))
}
g <- function(x = 7, y){
z <- 3
f()
}
g(99)
## [1] 102
g(99, 1000)
## [1] 1102
我遇到了同样的问题:想要一个函数来检查参数是否丢失。我还首先尝试了基于 eval
的想法,即使缺少变量,它也总是给我 FALSE
。
Josh 在上面提供了一个解决方案,但它太具体了。我想要的是一个简洁的检查器函数,我可以将其添加到我的函数中,以便它们检查必要变量中的缺失并抛出信息性错误。这是我的解决方案:
check_missing = function(var_names, error_msg = "[VAR] was missing! Please supply the input and try again.") {
#parent.frame as list
pf = as.list(parent.frame())
#check each if missing
for (name in var_names) {
#is it there at all?
if (!name %in% names(pf)) {
stop(name + " is not even found in the parent.frame! Check the variable names!", call. = F)
}
#check if missing
if (are_equal(pf[[name]], quote(expr = ))) {
stop(str_replace(error_msg, pattern = "\[VAR\]", name), call. = F)
}
}
#all fine
return(invisible(NULL))
}
要测试它,使用这个:
test_func = function(y) {
check_missing("y")
print("OK")
}
测试一下:
test_func(y = )
# Error: y was missing! Please supply the input and try again.
# Called from: check_missing("y")
test_func(y = "k")
# [1] "OK"
剩下不满意的地方就是报错了错误的"Called from"信息。它 returns 检查器函数本身,但如果它返回父函数,它会提供更多信息。不知道能不能解决。
希望这可能对某人有用。