[Rcpp-devel] How to define a function called when R exited

Jamie Olson jamie.f.olson at gmail.com
Tue May 6 19:02:19 CEST 2014

I believe you're looking for `.Last`. Unfortunately there's no way to
set `on.exit(...)` for the global environment.

>From the ?quit documentation:

Immediately before terminating, .Last() is executed if the function
.Last exists and runLast is true. If in interactive use there are
errors in the .Last function, control will be returned to the command
prompt, so do test the function thoroughly. There is a system
analogue, .Last.sys(), which is run after .Last() if runLast is true.

Exactly what happens at termination of an R session depends on the
platform and GUI interface in use. A typical sequence is to run
.Last() and .Last.sys() (unless runLast is false), to save the
workspace if requested (and in most cases also to save the session
history: see savehistory), then run any finalizers (see reg.finalizer)
that have been set to be run on exit, close all open graphics devices,
remove the session temporary directory and print any remaining
warnings (e.g. from .Last() and device closure).

Jamie Olson

On Tue, May 6, 2014 at 12:53 PM, Jerome MARQUET
<jerome.marquet at lixoft.net> wrote:
> Hello,
> I am building a package "myModule" and would like to define a function that
> will be called when R is exited.
> So I defined a function
> RcppExport void R_unload_mlxComputeR(DllInfo *info)
> {
>     // Do sthg
> }
> and I observe that it is called when dyn.unload(myModule) is called. But the
> function is not called when R is exited. I wonder if there is a similar
> function (like R_exit_packgName or R_quit_packageName) that is called when R
> is exited ?
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

More information about the Rcpp-devel mailing list