on.exit {base} | R Documentation

## Function Exit Code

### Description

`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` is a primitive function so positional matching is used and names of supplied arguments are ignored.

### Usage

    on.exit(expr, add = FALSE)

### Arguments

`expr` | an expression to be executed.
`add` | if TRUE, add `expr` to be executed after any previously set expressions; otherwise (the default) `expr` will overwrite any previously set expressions.

### Value

Invisible `NULL`.

### References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) The New S Language. Wadsworth & Brooks/Cole.

### See Also

`sys.on.exit` which returns the expression stored for use by `on.exit()` in the function in which `sys.on.exit()` is evaluated.

### Examples

    require(graphics)

    opar <- par(mai = c(1,1,1,1))
    on.exit(par(opar))

[Package base version 2.6.2 Index]