exists(x, where = -1, envir = if (missing(frame)) as.environment(where) else sys.frame(frame), frame, mode = "any", inherits = TRUE) get0(x, envir = pos.to.env(-1L), mode = "any", inherits = TRUE, ifnotfound = NULL)
x | a character string representing the object to be found/gotten. |
where | specifies the environment in which to find/get the object. exists accepts several ways to specify the environment. See Details for more information. |
envir | an environment object. An alternative way to specify the environment to find the object. |
frame | a frame used in calling list. If provided, the default value of envir is set to sys.frame(frame). |
mode | a character string giving the mode wanted for the object. The default, "any", means that any mode is acceptable. |
inherits |
a logical flag. If FALSE, the specified environment is searched. Normally, only the current frame,
the global frames, and databases are searched for an object.
If inherits=TRUE(the default), the parent frame of the current frame, its parent frame, and all further ancestors are searched. |
ifnotfound | a returned value if the given "x" object cannot be found the corresponding value. The default is NULL. |
exists | returns a logical value to indicate if the target object is found corresponding to the given name, mode, environment and other requirements. |
get0 | checks the existence of the object and, if the object is found, returns the value of the object. If the object is not found, returns ifnotfound. |
exists("CO2", where="package:stats") # [1] TRUE
exists("CO2", where=2) # [1] TRUE
if(exists("ls")) get("ls") else NULL get0("ls") # equivalent to above.
if(exists("foo_object")) get("foo_object") else NULL get0("foo_object") # equivalent to above.
get("foo_object") # with errors