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:
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:
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:
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.