Example of on.exit, add argument

r

Solution

Here is a very simple example

myfun <- function(x){
    on.exit(print("first"))
    on.exit(print("second"), add = TRUE)
    return(x)
}

myfun(2)
#[1] "first"
#[1] "second"
#[1] 2

Also note what happens without the add=TRUE parameter

fun <- function(x){
    on.exit(print("first"))
    on.exit(print("second"))
    return(x)
}

fun(2)
#[1] "second"
#[1] 2

The second call to on.exit removes the first call if you don't add the "add=TRUE".

Problem

Can someone provide me with a simple usage case where the on.exit() function's "add" argument is true?

Original source