Re: [Rd] unloadNamespace (Was: How to unload a dll loaded via library.dynam()?)

From: Berwin A Turlach <>
Date: Fri, 20 Feb 2009 18:02:05 +0800

G'day all,

On Fri, 20 Feb 2009 04:01:07 +0000 (GMT) Prof Brian Ripley <> wrote:

> library.dynam.unload() does work if the OS is cooperative. And if
> you have your package set up correctly and unload the namespace (and
> not just detach the package if there is a namespace) then the shared
> object/DLL will be unloaded. [...]

I guess I have a similar code-install-test development cycle as Alex; and I seem to work on a cooperative OS (Kubuntu 8.04).

My set up is that I install packages on which I work into a separate library. To test changes to such packages, I start R in a directory which contains a .Rprofile file which, via .libPaths(), adds the above library to the library path. In this R session I then test the changes.

I also used to quit and restart R whenever I re-installed a package with namespace to test the changes made. Somehow I got the impression that this was the way to proceed when namespaces were introduced; and I did not realise until recently that better ways (unloading the namespace) exist.

However, I noticed the following behaviour under R 2.8.1 and "R version 2.9.0 Under development (unstable) (2009-02-19 r47958)" which I found surprising:

  1. In the running R session, issue the command "unloadNamespace(XXX)"
  2. Do changes to the code of the package; e.g. add a "print("hello world")" statement to one of the R functions.
  3. Install the new package
  4. In the running R session, issue the command "library(XXX)" and call the R function that was changed.

Result: "Hello world" is not printed, somehow the old R function is still used. If I issue the commands "unloadNamespace(XXX)" and "library(XXX)" once more then a call to the R function that was changed will print "Hello world"; i.e. the new code is used.

If the above sequence is changed to 2), 3) and then 1), then 4) behaves "as expected" and the new R code is used immediately.

As far as I can tell, if via the .onUnload() hook the shared object is unloaded via library.dynam.unload(), changes in the C code take effect no matter whether I perform the above steps in the sequence 1-2-3-4 or 2-3-1-4.

My preference is to use the sequence 1-2-3-4 since it seems to be the "more logical and cleaner" sequence; and I have vague memories that I managed to crash R in the past after using 2-3 and then trying to quit R.

I am wondering why does it make a difference with respect to R code in which order these steps are done but not with respect to compiled code. Well, I guess I understand why the order does not matter for compiled code, but I do not understand why the order matters for R code. I could not find anything in the documentation that would explain this behaviour, or indicate that this is the intended behaviour.

Enlightening comments and/or pointers to where this behaviour is documented would be welcome.


        Berwin mailing list Received on Fri 20 Feb 2009 - 09:05:44 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 20 Feb 2009 - 11:30:30 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.

list of date sections of archive