This question already has answers here:
How to check if object (variable) is defined in R?

(6个答案)


4年前关闭。




我想确定对象是否存在于R中的函数中:
foo <- function() {
 y <- "hello"
 if (exists(y, envir = sys.frame())) print(y)
}
foo()



我以为添加envir = sys.frame()可以解决问题。也尝试过envir = environment()
预期的
foo()
"hello"

最佳答案

您应该已经检查?exists:

Usage:

     exists(x, where = -1, envir = , frame, mode = "any",
            inherits = TRUE)

Arguments:

       x: a variable name (given as a character string).

exists("y")

关于r - 验证对象在R中的函数中是否存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37471992/

10-12 23:35