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

Reply via email to