EVOLUTION-MANAGER
Edit File: exists.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Is an Object Defined?</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for exists {base}"><tr><td>exists {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Is an Object Defined?</h2> <h3>Description</h3> <p>Look for an <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> object of the given name and possibly return it </p> <h3>Usage</h3> <pre> exists(x, where = -1, envir = , frame, mode = "any", inherits = TRUE) get0(x, envir = pos.to.env(-1L), mode = "any", inherits = TRUE, ifnotfound = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>x</code></td> <td> <p>a variable name (given as a character string).</p> </td></tr> <tr valign="top"><td><code>where</code></td> <td> <p>where to look for the object (see the details section); if omitted, the function will search as if the name of the object appeared unquoted in an expression.</p> </td></tr> <tr valign="top"><td><code>envir</code></td> <td> <p>an alternative way to specify an environment to look in, but it is usually simpler to just use the <code>where</code> argument.</p> </td></tr> <tr valign="top"><td><code>frame</code></td> <td> <p>a frame in the calling list. Equivalent to giving <code>where</code> as <code>sys.frame(frame)</code>.</p> </td></tr> <tr valign="top"><td><code>mode</code></td> <td> <p>the mode or type of object sought: see the ‘Details’ section.</p> </td></tr> <tr valign="top"><td><code>inherits</code></td> <td> <p>should the enclosing frames of the environment be searched?</p> </td></tr> <tr valign="top"><td><code>ifnotfound</code></td> <td> <p>the return value of <code>get0(x, *)</code> when <code>x</code> does not exist.</p> </td></tr> </table> <h3>Details</h3> <p>The <code>where</code> argument can specify the environment in which to look for the object in any of several ways: as an integer (the position in the <code><a href="search.html">search</a></code> list); as the character string name of an element in the search list; or as an <code><a href="environment.html">environment</a></code> (including using <code><a href="sys.parent.html">sys.frame</a></code> to access the currently active function calls). The <code>envir</code> argument is an alternative way to specify an environment, but is primarily there for back compatibility. </p> <p>This function looks to see if the name <code>x</code> has a value bound to it in the specified environment. If <code>inherits</code> is <code>TRUE</code> and a value is not found for <code>x</code> in the specified environment, the enclosing frames of the environment are searched until the name <code>x</code> is encountered. See <code><a href="environment.html">environment</a></code> and the ‘R Language Definition’ manual for details about the structure of environments and their enclosures. </p> <p><b>Warning:</b> <code>inherits = TRUE</code> is the default behaviour for <span style="font-family: Courier New, Courier; color: #666666;"><b>R</b></span> but not for S. </p> <p>If <code>mode</code> is specified then only objects of that type are sought. The <code>mode</code> may specify one of the collections <code>"numeric"</code> and <code>"function"</code> (see <code><a href="mode.html">mode</a></code>): any member of the collection will suffice. (This is true even if a member of a collection is specified, so for example <code>mode = "special"</code> will seek any type of function.) </p> <h3>Value</h3> <p><code>exists():</code> Logical, true if and only if an object of the correct name and mode is found. </p> <p><code>get0():</code> The object—as from <code><a href="get.html">get</a>(x, *)</code>— if <code>exists(x, *)</code> is true, otherwise <code>ifnotfound</code>. </p> <h3>Note</h3> <p>With <code>get0()</code>, instead of the easy to read but somewhat inefficient </p> <pre> if (exists(myVarName, envir = myEnvir)) { r <- get(myVarName, envir = myEnvir) ## ... deal with r ... } </pre> <p>you now can use the more efficient (and slightly harder to read) </p> <pre> if (!is.null(r <- get0(myVarName, envir = myEnvir))) { ## ... deal with r ... } </pre> <h3>References</h3> <p>Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) <em>The New S Language</em>. Wadsworth & Brooks/Cole. </p> <h3>See Also</h3> <p><code><a href="get.html">get</a></code> and <code><a href="../../utils/html/hasName.html">hasName</a></code>. For quite a different kind of “existence” checking, namely if function arguments were specified, <code><a href="missing.html">missing</a></code>; and for yet a different kind, namely if a file exists, <code><a href="files.html">file.exists</a></code>. </p> <h3>Examples</h3> <pre> ## Define a substitute function if necessary: if(!exists("some.fun", mode = "function")) some.fun <- function(x) { cat("some.fun(x)\n"); x } search() exists("ls", 2) # true even though ls is in pos = 3 exists("ls", 2, inherits = FALSE) # false ## These are true (in most circumstances): identical(ls, get0("ls")) identical(NULL, get0(".foo.bar.")) # default ifnotfound = NULL (!) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>