From ripley at stats.ox.ac.uk  Fri Feb 20 05:01:07 2009
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Fri, 20 Feb 2009 04:01:07 +0000 (GMT)
Subject: [Rd] [R] How to unload a dll loaded via library.dynam()?
In-Reply-To: <d70c15d40902191643v4cba511ds794869eaf51d20d2@mail.gmail.com>
References: <30070-82496@sneakemail.com> <499DFA13.4060600@stats.uwo.ca>
	<d70c15d40902191643v4cba511ds794869eaf51d20d2@mail.gmail.com>
Message-ID: <alpine.LFD.2.00.0902200338010.10231@gannet.stats.ox.ac.uk>

[Moved to R-devel as suggested.]

library.dynam.unload() does work if the OS is cooperative.  And if you 
have your package set up correctly and unload the namespace (and not 
just detach the package if there is a namespace) then the shared 
object/DLL will be unloaded. There are examples in several base R 
packages (grDevices, grid, methods, splines, stats, tools).

However, there are several instances on Windows (tcltk is one) where 
reloading the package DLL does not work, and this seems to be because 
dependent DLLs are already loaded and do not run their initializations 
correctly.  I've not seen problems on Linux.  On Solaris reloading a 
different shared object of the same name into a process used not to 
work (you got the first version under that name): I've not checked 
recently.

On Fri, 20 Feb 2009, G?bor Cs?rdi wrote:

> Hmmm, I think restarting R is not a very good solution. It is rather
> ugly in the first place. But perhaps even more importantly, it can be
> rather inconvenient if one has a large data set in the memory and
> needs to save/load it just to reload a package that was updated in the
> meanwhile.
>
> This happens to me quite often with 20-40Gb of data, so I would really
> love to have a solution for proper detach/unload.

Why not use the one already provided?

>
> Best,
> Gabor
>
> On Fri, Feb 20, 2009 at 1:32 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
>> On 19/02/2009 6:19 PM, Alex F. Bokov wrote:
>>>
>>> Hello. To save the hassle of quitting and restarting R every time I
>>> rebuild the package I'm working on (for the purposes of this question called
>>> "roots") I would like to write a script cleanly remove the package from my R
>>> session and then load it again. Of course detach("package:roots") works for
>>> the native R objects in the package. However, the compiled C library remains
>>> loaded. Using library.dynam.unload as documented results in:
>>>
>>> Error in library.dynam.unload("roots.so",
>>> "~/R/x86_64-pc-linux-gnu-library/2.8/roots/libs/") :
>>>  shared library 'roots' was not loaded
>>>
>>> I also tried the above command with the file extension omitted and the
>>> last one and two levels of the directory path omitted. In both cases I still
>>> get the "not loaded" error. I've also Googled the list archives and while
>>> there were several questions like mine, none of them were answered by the
>>> list. Am I to believe that this is something hardly anybody does? Does this
>>> mean everybody sits around waiting for R to start up everytime they make a
>>> minor change to the C source?
>>>
>>> If there is a tutorial for doing this, I'd much appreciate the link.
>>
>>
>> This is more of an R-devel question than R-help, so if this doesn't answer
>> your question, please follow up there.
>>
>> On Windows, the following sort of works:
>>
>>> library(rgl)
>>> .dynLibs()
>>                                           Filename Dynamic.Lookup
>> 1     F:/R/R-2.8.1/library/methods/libs/methods.dll          FALSE
>> 2 F:/R/R-2.8.1/library/grDevices/libs/grDevices.dll          FALSE
>> 3         F:/R/R-2.8.1/library/stats/libs/stats.dll          FALSE
>> 4             F:/R/R-2.8.1/library/rgl/libs/rgl.dll           TRUE
>>
>>> library.dynam.unload("rgl", "F:/R/R-2.8.1/library/rgl")
>>
>> I say "sort of", in that I get no error messages and the dll is no longer
>> locked in use as it normally would be in Windows, but reloading the package
>> doesn't function properly. It's possible some combination of detach() and
>> unloadNamespace() would get a clean unload, but really, I normally just quit
>> R and restart.  It's pretty quick to start up, compared to the recompile
>> time for the package.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>
>
> -- 
> Gabor Csardi <Gabor.Csardi at unil.ch>     UNIL DGM
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595


[Rd] [R] How to unload a dll loaded via library.dynam()?
Prof Brian Ripley