Tuesday, November 29, 2011

Unloading dynamic libraries when detaching a package

This may be common knowledge for R gurus, but it took me a couple of hours to figure out.

Suppose you are writing a package "myPackage". Your package contains some C/C++ code that gets compiled into a dynamic library "myPackage.so" on unix-like systems. You also have an R script that you use to test your package, which starts with the following code:

if (is.element("myPackage", .packages())){
  detach(package:myPackage)
}
require(myPackage, lib.loc="my_local_R_libs")
Created by Pretty R at inside-R.org

The code fist checks if your package is loaded and if this is the case, the code detaches the packages before loading it again.

This will work fine if you are not changing anything in the /src directory of your package. But it turns out that detaching a package does not unload the corresponding dynamic library. To do both you need to do two things:

1. Create an R script in the /R directory (e.g. hooks.R) that defines the function:

.onUnload <- function(libpath) { library.dynam.unload("myPackage", libpath) }
Created by Pretty R at inside-R.org

    This tells R to unload the dynamic library of the package.
    Beware that this works only for packages that use namespaces.

2. Modify your test R script by adding unload="TRUE" argument to the detach function:
if (is.element("myPackage", .packages())){
       detach(package:myPackage, unload="TRUE")
    }
 require(myPackage, lib.loc="my_local_R_libs")
Created by Pretty R at inside-R.org

Now every time you re-install your package with R CMD INSTALL /myPackage, your test script will be using the updated dynamic library if you modify anything in the /src directory.