[R] The lib.loc argument to library().

Rolf Turner r.turner at auckland.ac.nz
Tue Mar 18 23:45:40 CET 2014


On 19/03/14 10:59, Prof Brian Ripley wrote:
> On 18/03/2014 21:41, Rolf Turner wrote:
>>
>>
>> I am currently having, uh, difficulties, with the latest version of
>> lme4, which I did not have with an elderly version that I was using
>> previously.  To check things out I installed the elderly version in a
>> directory called "AltRlib", in my home directory.
>>
>> I kept the latest version (1.1-5) in a directory called "Rlib" in my
>> home directory.
>>
>> I have R_LIBS set equal to "/home/rolf/Rlib" in my environment, so that
>> "Rlib" gets searched automatically.
>>
>> If I start R in my home directory and issue the call
>>
>>      library(lme4,lib.loc="/home/rolf/AltRlib")
>>
>> then I get the elderly version as required.
>>
>> If I start R in a subdirectory, say "/home/Rolf/Foo" and issue
>> the *same command*, I get the 1.1-5 version, *NOT* as required.
>>
>> After some head-scratching I moved the .RData file in "Foo" to
>> Save.RData and re-started R.  ***Then*** I got the version of lme4 that
>> I wanted.
>>
>> So I removed all traces of results produced by the 1.1-5 version of lme4
>> from Save.RData (saving them elsewhere for safekeeping), moved
>> Save.RData back to .RData, re-started R, issued the library command, and
>> got the unwanted 1.1-5 version!!!
>>
>> Can anyone explain WTF is going on?  What is hanging around in .RData
>> that causes library() to ignore the "lib.loc" argument?  How can I keep
>> library() from ignoring the "lib.loc" argument?
>
> It seems you have already loaded the namespace 'lme4' before you issue
> the library() call.
>
> Try loadedNamespaces() to confirm.
>
> As the help says
>
>       ‘library(package)’ and ‘require(package)’ both load the package
>       with name ‘package’ and put it on the search list.  ‘require’ is
>       designed for use inside other functions; it returns ‘FALSE’ and
>       gives a warning (rather than an error as ‘library()’ does by
>       default) if the package does not exist.  Both functions check and
>       update the list of currently loaded packages and do not reload a
>       package which is already loaded.

Thanks Brian.  That was indeed the problem.  Doing loadedNamespaces() 
before issuing any library() call did indeed reveal that lme4 was among 
the loaded namespaces.  I found that issuing the command 
unloadNamespace("lme4") and then issuing the library() command with 
lib.loc specified got me the elderly version of lme4 that I wanted.

So I can now carry on.

It appears that the presence of "lme4 objects" of *whatever* vintage is 
triggering the loading of the "lme4" namespace, and the namespace is (of 
course?!?) that of the version that appears in one of the directories in 
.libPaths().  Seems obvious in retrospect.  Given that one is aware that 
the presence of "lme4 objects" triggers the loading of the "lme4" namespace.

cheers,

Rolf




More information about the R-help mailing list