[Rd] Finding dynamic shared libraries loaded with a package

2012-07-23 Thread Winston Chang
Is there a way to query a package to see what dynamic shared libraries are
loaded with it?


The reason I ask is because during development, I want to unload libraries
so that they can be reloaded without restarting R. I want to make it
automatic so that you can just pass in the name of the package, and it will
unload all the relevant shared libraries.

Typically, the name of the shared library is the same as the package. So
something like this usually works:
pkgname - 'bitops'
pkgpath - system.file(package=pkgname)
library.dynam.unload(pkgname, pkgpath)


Some R packages have shared libraries with names that differ from the
package, and this strategy won't work for them. I'm aware that the
NAMESPACE file will have an entry like this:
useDynLib(libname)

but I don't know how to access this information from within R. Is this
possible?


Another strategy I've looked at is to get all the directories listed by
.dynLibs() and picking out those that contain the path of the package, but
I'd prefer not to do it this way if possible, since it seems like a bit of
a hack. For example, this code will load bitops, then unload the shared
library and unload the package.


library(bitops)
# Show what's loaded
.dynLibs()

pkgname - 'bitops'

# Get installation path for the package
pkgpath - system.file(package=pkgname)

# Get a vector of paths for all loaded libs
dynlib_paths - vapply(.dynLibs(), function(x) x[[path]], character(1))

# Find which of the lib paths start with pkgpath
pkgmatch - pkgpath == substr(dynlib_paths, 1, nchar(pkgpath))

# Get matching lib paths and strip off leading path and extension (.so or
.dll)
libnames - sub(\\.[^\\.]*$, , basename(dynlib_paths[pkgmatch]))

library.dynam.unload(libnames, pkgpath)

# Show what's loaded
.dynLibs()

# Finally, also unload the package
detach(paste(package, pkgname, sep =:), character.only = TRUE,
  force = TRUE, unload = TRUE)


Thanks for any help you can provide,
-Winston

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Finding dynamic shared libraries loaded with a package

2012-07-23 Thread Gabor Grothendieck
On Mon, Jul 23, 2012 at 8:29 PM, Winston Chang winstoncha...@gmail.com wrote:
 Is there a way to query a package to see what dynamic shared libraries are
 loaded with it?


This gives a DLLInfoList class object whose components are info
associated with the loaded dll's

DLLInfoList - library.dynam()

and this gives the components associated with package stats

DLLInfoList[sapply(DLLInfoList, [[, name) == stats]





-- 
Statistics  Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] Finding dynamic shared libraries loaded with a package

2012-07-23 Thread Winston Chang
On Mon, Jul 23, 2012 at 7:47 PM, Gabor Grothendieck ggrothendi...@gmail.com
 wrote:

 On Mon, Jul 23, 2012 at 8:29 PM, Winston Chang winstoncha...@gmail.com
 wrote:
  Is there a way to query a package to see what dynamic shared libraries
 are
  loaded with it?
 

 This gives a DLLInfoList class object whose components are info
 associated with the loaded dll's

 DLLInfoList - library.dynam()

 and this gives the components associated with package stats

 DLLInfoList[sapply(DLLInfoList, [[, name) == stats]


Thanks - I think this does the trick!

Although I decided to use .dynLibs() instead of library.dynam(). The latter
just calls the former when no name is passed to it.


Another mailing list member sent me a message suggesting getLoadedDLLs().
This appears to be slightly different -- if I understand correctly, it
returns all loaded DLLs, while .dynLibs() returns just the ones loaded by
packages.

-Winston

[[alternative HTML version deleted]]

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel