on.exit 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. If no expression is provided, i.e., the call is on.exit(), then the current on.exit code is removed.
on.exit(expr = NULL, add = FALSE, after = TRUE)
| Parameter | Description |
|---|---|
expr |
an expression to be executed. |
add |
if TRUE, add expr to be executed after any previously set expressions (or before if after is FALSE); otherwise (the default) expr will overwrite any previously set expressions. |
after |
if add is TRUE and after is FALSE, then expr 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. |
# NOT RUN {
require(graphics)
opar <- par(mai = c(1,1,1,1))
on.exit(par(opar))
# }