tags:

views:

51

answers:

0

when I load a library (with a NAMESPACE), the functions .onLoad and .onAttach are called, as is .onUnload when I detach the library unloading the namespace.

I was wondering whether R does define a way that would save me the work of detaching/unloading the library by hand in each of my scripts that use the xxx library.

for this I would need a library hook that is checked and invoked when a script using the library ends, if there is any such thing. I didn't find it, and I always imagine there is a reason why things are there as well as why they are not.

I understand, from the help files and from the comments, that there is a .Last script hook I can use, but I am looking for something similar to constructor/destructor: as soon as the library "goes out of scope" (because the script using it ends), the "library destructor" would be invoked.


still in other words, I am wondering whether it is at all possible that a script with really only the two lines

#!/usr/bin/Rscript
library(xxx)

and a library xxx with a NAMESPACE and a file zzz.R containing among other stuff this

.onLoad <- function(libpath, pkgname) {
  packageStartupMessage("loading ", libpath, '::', pkgname)
}

.onUnload <- function(pkgpath) {
  packageStartupMessage("unloading ", pkgpath)
}

produces this output

loading /usr/local/lib/R/site-library::xxx
unloading /usr/local/lib/R/site-library/xxx

or if I need to explicitly call detach('package:NenS', unload=TRUE) in each script using the library xxx.