我想确定一个对象是否存在于R中的函数内:
foo <- function() {
y <- "hello"
if (exists(y, envir = sys.frame())) print(y)
}
foo()
exists(y,envir = sys.frame())出错:第一个参数无效
我认为添加envir = sys.frame()
就可以了。还尝试了envir = environment()
预期
foo()
"hello"
答案 0 :(得分:4)
您应该检查?exists
:
Usage:
exists(x, where = -1, envir = , frame, mode = "any",
inherits = TRUE)
Arguments:
x: a variable name (given as a character string).
执行exists("y")