EVOLUTION-MANAGER
Edit File: on.exit.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: Function Exit Code</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 on.exit {base}"><tr><td>on.exit {base}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2>Function Exit Code</h2> <h3>Description</h3> <p><code>on.exit</code> records the expression given as its argument as needing to be executed when the current function exits (either naturally or as the result of an error). This is useful for resetting graphical parameters or performing other cleanup actions. </p> <p>If no expression is provided, i.e., the call is <code>on.exit()</code>, then the current <code>on.exit</code> code is removed. </p> <h3>Usage</h3> <pre> on.exit(expr = NULL, add = FALSE, after = TRUE) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>expr</code></td> <td> <p>an expression to be executed.</p> </td></tr> <tr valign="top"><td><code>add</code></td> <td> <p>if TRUE, add <code>expr</code> to be executed after any previously set expressions (or before if <code>after</code> is FALSE); otherwise (the default) <code>expr</code> will overwrite any previously set expressions.</p> </td></tr> <tr valign="top"><td><code>after</code></td> <td> <p>if <code>add</code> is TRUE and <code>after</code> is FALSE, then <code>expr</code> will be added on top of the expressions that were already registered. The resulting last in first out order is useful for freeing or closing resources in reverse order.</p> </td></tr> </table> <h3>Details</h3> <p>The <code>expr</code> argument passed to <code>on.exit</code> is recorded without evaluation. If it is not subsequently removed/replaced by another <code>on.exit</code> call in the same function, it is evaluated in the evaluation frame of the function when it exits (including during standard error handling). Thus any functions or variables in the expression will be looked for in the function and its environment at the time of exit: to capture the current value in <code>expr</code> use <code><a href="substitute.html">substitute</a></code> or similar. </p> <p>If multiple <code>on.exit</code> expressions are set using <code>add = TRUE</code> then all expressions will be run even if one signals an error. </p> <p>This is a ‘special’ <a href="Primitive.html">primitive</a> function: it only evaluates the argument <code>add</code>. </p> <h3>Value</h3> <p>Invisible <code>NULL</code>. </p> <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="sys.parent.html">sys.on.exit</a></code> which returns the expression stored for use by <code>on.exit()</code> in the function in which <code>sys.on.exit()</code> is evaluated. </p> <h3>Examples</h3> <pre> require(graphics) opar <- par(mai = c(1,1,1,1)) on.exit(par(opar)) </pre> <hr /><div style="text-align: center;">[Package <em>base</em> version 3.6.0 <a href="00Index.html">Index</a>]</div> </body></html>