From ripley at stats.ox.ac.uk  Mon Apr 10 12:16:10 2006
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Mon, 10 Apr 2006 11:16:10 +0100 (BST)
Subject: [Rd] Run package code on R shutdown?
In-Reply-To: <4439A4F2.3040708@stats.uwo.ca>
References: <4439A4F2.3040708@stats.uwo.ca>
Message-ID: <Pine.LNX.4.64.0604101107320.22272@gannet.stats.ox.ac.uk>

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,                  ripley at stats.ox.ac.uk
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



[Rd] Run package code on R shutdown?
Prof Brian Ripley