Run package code on R shutdown?

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Run package code on R shutdown?

Duncan Murdoch
I'm sure I've seen this discussed before, but haven't been able to find
it.  I'd like some package code to be run when R is shut down
(approximately when a user's .Last function would be run), to clean up
properly.  What is the best way to do this?

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Run package code on R shutdown?

Brian Ripley
On Sun, 9 Apr 2006, Duncan Murdoch wrote:

> I'm sure I've seen this discussed before, but haven't been able to find
> it.  I'd like some package code to be run when R is shut down
> (approximately when a user's .Last function would be run), to clean up
> properly.  What is the best way to do this?

The only way I know to do this is to use a finalizer, as we don't run
.Last.lib on shutdown.  (That's how RODBC does it.)

Now, as I recall this cannot be done from reg.finalizer, only from the
C-level R_RegisterCFinalizerEx, which has an optional argument to ensure
that the finalizer is run 'onexit'.   (I have never understood why we have
that restriction, nor why reg.finalizer is primitive and not .Internal.)

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Run package code on R shutdown?

Henrik Bengtsson-2
In reply to this post by Duncan Murdoch
On 4/10/06, Duncan Murdoch <[hidden email]> wrote:
> I'm sure I've seen this discussed before, but haven't been able to find
> it.  I'd like some package code to be run when R is shut down
> (approximately when a user's .Last function would be run), to clean up
> properly.  What is the best way to do this?

I tried to do this some time ago.  My conclusion then is that it
cannot be done with a guarantee, because R can exit in different ways.
 I implemented what I had an came up with an onSessionExit() method
available in R.utils.  Check that out for a start.  It modifies
.Last(), but that can be circumvented by quit(callLast=FALSE).

/Henrik

> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>


--
Henrik Bengtsson
Mobile: +46 708 909208 (+2h UTC)

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Run package code on R shutdown?

Duncan Murdoch
In reply to this post by Brian Ripley
On 4/10/2006 6:16 AM, Prof Brian Ripley wrote:

> On Sun, 9 Apr 2006, Duncan Murdoch wrote:
>
>> I'm sure I've seen this discussed before, but haven't been able to find
>> it.  I'd like some package code to be run when R is shut down
>> (approximately when a user's .Last function would be run), to clean up
>> properly.  What is the best way to do this?
>
> The only way I know to do this is to use a finalizer, as we don't run
> .Last.lib on shutdown.  (That's how RODBC does it.)
>
> Now, as I recall this cannot be done from reg.finalizer, only from the
> C-level R_RegisterCFinalizerEx, which has an optional argument to ensure
> that the finalizer is run 'onexit'.   (I have never understood why we have
> that restriction, nor why reg.finalizer is primitive and not .Internal.)

Thanks!

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel