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")
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) }
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")
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.