Analogously to the newly added on_session_enter(), add a startup::on_session_exit() that makes it simple to run things at the end of the current R session. It should take both R expressions and R functions as input, e.g.
startup::on_session_exit(quote({
message("Exiting R ...")
}))
startup::on_session_exit(local({
t0 <- Sys.time()
function(...) {
dt <- difftime(Sys.time(), t0, units = "auto")
message(gettextf("R session exited after %.2f %s", dt, attr(dt, "units")))
}
})
which will give:
> quit("no")
Exiting R ...
R session exited after 1.62 mins
Implementation
This should rely on reg.finalizer().
Misc.
We could also add an on_last(), that will work out of .Last(), which will only run when quit(runLast = TRUE) is called (runLast = TRUE is the default).
Analogously to the newly added
on_session_enter()
, add astartup::on_session_exit()
that makes it simple to run things at the end of the current R session. It should take both R expressions and R functions as input, e.g.which will give:
Implementation
This should rely on
reg.finalizer()
.Misc.
We could also add an
on_last()
, that will work out of.Last()
, which will only run whenquit(runLast = TRUE)
is called (runLast = TRUE
is the default).(*) Rename to
on_first()
?