This question already has answers here:
Closed 3 years ago.
I want to determine whether an object exists inside a function in R:
foo <- function() {
y <- "hello"
if (exists(y, envir = sys.frame())) print(y)
}
foo()
Error in exists(y, envir = sys.frame()) : invalid first argument
I thought adding the envir = sys.frame()
would do the trick. Also tried envir = environment()
Expected
foo()
"hello"