Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of on.exit, add argument

Tags:

r

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

like image 372
Brandon Bertelsen Avatar asked Jun 11 '12 00:06

Brandon Bertelsen


1 Answers

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".

like image 100
Dason Avatar answered Sep 22 '22 18:09

Dason