On.exit example, add argument

Can someone provide me with a simple use case where the argument to the add function on.exit () is correct?

+4
source share
2 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 do not add "add = TRUE".

+7
source

I do not know R, but I read the definition of onexeit , add = true , so that the first expression is evaluated in addition to any previously saved single protrusions, and add = false overwrites the previous one.

So, looking back, I found this on jhsph.edu :

It seems like basically making sure tmp also closed, and file on exit

 readData2 <- function(file, ...) { file <- file(file, "r") on.exit(close(file)) tmp <- tempfile() tmpcon <- file(tmp, "w") on.exit(close(tmpcon), add = TRUE) incomment <- FALSE while(length(line <- readLines(file, 1)) > 0) { . . . ommited in SO example . . . } close(tmpcon) close(file) on.exit() read.csv(tmp, ...) } 
0
source

All Articles