From murdoch at stats.uwo.ca  Thu Jan 18 13:02:12 2007
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Thu, 18 Jan 2007 07:02:12 -0500
Subject: [Rd] on.exit called on loading ?
In-Reply-To: <17837.19931.177286.821128@basebud.nulle.part>
References: <17837.15696.636229.572815@basebud.nulle.part>	<45AD45F6.4070003@stats.uwo.ca>
	<17837.19931.177286.821128@basebud.nulle.part>
Message-ID: <45AF61C4.6040803@stats.uwo.ca>

On 1/16/2007 5:12 PM, Dirk Eddelbuettel wrote:
> On 16 January 2007 at 16:39, Duncan Murdoch wrote:
> | On 1/16/2007 4:02 PM, Dirk Eddelbuettel wrote:
> | > I just found out that an .onLoad() function such as this stylized one (where
> | > I just renamed some identifiers)
> | > 
> | > 
> | > .onLoad <- function(lib, pkg) {
> | >   require(zoo, quiet=TRUE, warn.conflicts=FALSE)
> | >   library.dynam("foolib", pkg, lib )
> | >   if (.Platform$OS.type != "windows") {
> | >     initSomeServices()
> | >   }
> | >   if (.Platform$OS.type != "windows") {
> | >     on.exit(closeSomeServices())
> | >   }
> | > }
> | > 
> | > actually triggers a call of 'closeSomeServices()'. I am probably
> | > misunderstanding something here -- but I thought on.exit() would only be
> | > called on, well, exit ?
> | 
> | It's the exit from the function, not the exit from the package (which 
> | isn't really all that well defined -- do you mean unloading, exit from 
> | R, detaching??
> 
> Thanks to Robert (off-list), Henrik and Duncan -- I had indeed forgotten /
> confused what on.exit() is for. Works as advertised here, but that wasn't
> what I wanted at the time. Entirely my bad.
>  
> | It's not very easy to have something be guaranteed to execute "when 
> | you're done".  The RODBC package does it using an external pointer, 
> | which has a hook that is called when R shuts down.  If it's good enough 
> | to execute on unloading but skip execution on shutdown, then .onUnload 
> | is available.
> 
> Yes, I am dealing with a moderately more complicated situation (of
> subscribing to some stateful internal 'services') and had not found 
> .onUnload to be as reliable as I had hoped. But that's another issue.

reg.finalizer is probably what you want.  Since 2.4.0 it has had an 
option to be called when R shuts down.

Duncan Murdoch



[Rd] on.exit called on loading ?
Duncan Murdoch