1

I'd like to do some cleanup work before the R process is exited by accidently. Is there any api like java's shutdown hook in R ? Thanks

zjffdu
  • 25,496
  • 45
  • 109
  • 159

1 Answers1

0

Defining a .Last function will do the trick.

.Last <- function() {
    printf("\n  == END of R | %s (runtime %s) ==\n", Sys.time(),format(round(difftime(Sys.time(), .r.start.time), 2)))
}

similarly .First function is called at the start of the script.

Alok
  • 3,160
  • 3
  • 28
  • 47
  • it seems .Last doesn't work for me. My scenario is that I am in R interactive session, and then I press ctrl + z to stop the R session. and I want to execute something before exit the R session. – zjffdu Dec 08 '16 at 08:09