[R] Load and unload libraries

Rita Ribeiro rita at liacc.up.pt
Tue Apr 1 12:43:57 CEST 2003

On Tue, 2003-04-01 at 10:53, Prof Brian Ripley wrote:
> On 1 Apr 2003, Rita Ribeiro wrote:
> > I'm having some problems in loading libraries. I wonder if anyone can
> > help me with this.
> > 
> > I have created two libraries with the same name at different locations.
> Do you mean packages?

Yes, sorry. 
Packages in tar.gz files which I have installed with R CMD INSTALL in
different locations, just to test different things.

The library locations are the paths to where the packages have been

Here's a small simulation:

> library(mylib,lib.loc1)
> .path.package("mylib") # returns lib.loc1
> detach('package:mylib')
> .path.package("rpart")
Error in .path.package(mylib) : none of the packages are loaded
> exists("mylib") # returns FALSE

> library(mylib,lib.loc2)
> .path.package("mylib") # returns lib.loc2
> .find.package("mylib") # returns lib.loc2

The problem is when I invoke mylib function: it uses the binary code
from the first library location.

Is there anyway of "unloading" the first library to load a new one,
although with the same name. Ie is there anyway of getting a "cleanup"

Should I give different names to the packages to solve the problem? I
preferred no to....

My system information:

platform i686-pc-linux-gnu
arch     i686             
os       linux-gnu        
system   i686, linux-gnu  
major    1                
minor    6.0              
year     2002             
month    10               
day      01               
language R       

> > I want to use both of them, one at a time.
> > 
> > So I do:
> > 
> > library(mylib,lib.loc1)
> > (....)
> > detach('package:mylib')
> > 
> > library(mylib,lib.loc2)
> > 
> > The problem is that, after this, the used library is still the one first
> > loaded.
> > 
> > Isn't detach command enough? 
> Not in general. detaching a package does not in general unload any 
> *library* (DLL, shared library, dynamic library, name depending on OS)
> that was loaded.  If there are no namespaces involved (a new feature of 
> 1.7.0) it should give you the second version of the R code, and it did so 
> when I just did an experiment.
> I'm afraid you will need to be more precise in your terminology and give 
> us more details.
Rita Ribeiro <rita at liacc.up.pt>

More information about the R-help mailing list