[R] shared object location

Murat Tasan mmuurr at gmail.com
Fri Jan 29 21:58:03 CET 2010


hi all, i posted a question before about this, but i may have been too
cryptic to understand.

in short, there exists an R package that someone is writing.  this
package depends on a custom library (written in C,), compiled as a
shared, and called by the package's functions via the .Call(...)
method.

we are testing out different code implementations of the compiled
library functions, and thus have distinct compiled shared object files
(.so files in UNIX-like systems).  currently this works just fine via
the R command prompt using the dyn.load(...) and dyn.unload(...)
functions.  consider the following scenario:

two libraries of the same functions (different implementations) exist
at a user-level directory (we don't have system /lib or /usr/lib
access).  these two files reside here:
~/lib/mylib.v1.so and ~/lib/mylib.v2.so
mylib.so is a copy (or link) to mylib.v1.so
start R, load the library via 'dyn.load("~/lib/mylib.so")'
execute R function that uses .Call(...) to a compiled function.
unload the library via 'dyn.unload("~/lib/mylib.so")'
copy mylib.v2.so to mylib.so
load the library via 'dyn.load("~/lib/mylib.so")'
execute R function again and compare results

(note that i know one could just load the specific versions of the
library in the above scenario, but it helps with consistency for the
following case.)

now, i'd like to have the R package written such that it expects the
library mylib.so, without having to load it explicitly via the dyn.load
(...) command.  the package has a NAMESPACE, so i've tried this
solution by including (in zzz.R) this function:

.onLoad <- function(libname, package) library.dynam("mylib")

however, this (and variants of it i've tried) doesn't seem to work as
R will only search for the shared objects in the R package directories
themselves (particularly the /libs/ sub-directories of each package,
or the one you specify).  my .so files sit external to an R package
directory, and also external to UNIX system-level libs.

(yes, i've set my LD_LIBRARY_PATH shell environment variable to
include ~/lib/, by the way.)

i've also tried the useDynLib(...) directive in the package NAMESPACE
file, but to no avail, as this seems to mimic library.dynam and only
search *inside* package contents for shared object libraries.

so, simply put:

i have a shared object library (compiled by R CMD SHLIB, to ensure
proper linking), in a user-level *local* lib directory (~/lib).  this
directory is on the LD_LIBRARY_PATH (as it is used to test
replacements for some system-level libraries).
i would like to have an R package that dynamically loads this shared
object library upon package loading (or attachment, if needed), as
functions in the package call methods in the library via the .Call
(...) procedure.
i'm happy always having my shared object library have a single name (i
can update the current one when switching between versions), and doing
so would allow for a single line in the R package code to dynamically
load the (whichever version is current) library.

problem is, i haven't been able to do this yet.

my workaround is to constantly swap into myRPackage/libs/ directory a
version of the shared object library called myRPackage.so, and load it
via the useDynLib(...) directive in the NAMESPACE file of the
package.  but this is cumbersome and doesn't allow other people to
have a single version of the R package to use as part of a team effort
to debug/test both the package's R code and my C code for the library
functions.

hopefully this makes a bit more sense, and if anyone has any tips on
how R actually loads shared objects it would be greatly appreciated.

so far all i can tell is that R will only look inside R packages in
the /libs/ directory, or at system-wide /lib/, /usr/lib/, etc.
directories for shared object files.  (unless one explicitly uses
dyn.load with a hard-coded path, in which case it loads the library
just fine, but this is not an elegant solution.)

thanks very much for any help!

-m



More information about the R-help mailing list