Learn R Programming

mvbutils (version 1.1.0)

local.on.exit: Macro-like functions

Description

local.on.exit is the analogue of on.exit for "nested" or "macro" functions written with mlocal.

Usage

# Inside an mlocal function of the form function( <>, nlocal=sys.parent(), <>) mlocal({
local.on.exit( expr, add=FALSE)

Arguments

expr
the expression to evaluate when the function ends
add
if TRUE, the expression will be appended to the existing local.on.exit expression. If FALSE, the latter is overwritten.

Details

on.exit doesn't work properly inside an mlocal function, because the scoping is wrong (though sometimes you get away with it). Use local.on.exit instead, in exactly the same way. I can't find any way to set the exit code in the calling function from within an mlocal function. Exit code will be executed before any temporary variables are removed (see mlocal).

See Also

mlocal, local.return, local.on.exit, do.in.envir, and R-news 1/3

Examples

Run this code
ffin <- function( nlocal=sys.parent(), x1234, yyy) mlocal({
 x1234 <- yyy <- 1 # x1234 & yyy are temporary variables
 # on.exit( cat( yyy)) # would crash after not finding yyy
 local.on.exit( cat( yyy))
 })
ffout <- function() {
 x1234 <- 99
 ffin()
 x1234 # still 99 because x1234 was temporary
}
ffout()

Run the code above in your browser using DataLab