[R] Relationship between dyn.load and library.dynam

Campbell p.campbell at econ.bbk.ac.uk
Thu Jun 30 17:11:05 CEST 2005


I am a little confused about the relationship between library.dynam and
dyn.load

>From the documentation:

library.dynam(chname,

Load the specified file of compiled code if it has not been loaded
already, or unloads it. Where chname is a character string naming a
shared library to load.

and

dyn.load(x, local = TRUE, now = TRUE)

Load or unload shared libraries, and test whether a C function or
Fortran subroutine is available. Where x is a character string giving
the pathname to a shared library or DLL.

Perhaps the problems is caused by the relationship between 'shared
libraries' and 'complied code'.  Given 
that the compiled code is not an executable then it must be a shared
library. 

Both these functions would appear to do the same thing; load code
complied using R CMD SHLIB into R's 
memory space and and make the functions that are defined in the code
available to R.  In Windows there 
are 2 distinct operations: loading the shared object and then making the
names of the functions that
the object exports available to applications.

What I am trying to do is create a compiled shared library that is able
to access the SEXP data type.
To do this I wrtie the code in C, run R CMD SHLIB to generate the file
MyOut.so.  I then build a package
with the following function:

.First.lib<-function(lib, pkg){ 
  dyn.load(x="/home/phineas/pkg/CallRFromC/R/MyOut.so")
  library.dynam(chname="MyOut")
}

The use of hard wired paths is to simplify development

When I run R CMD check on the package I get 


* checking S3 generic/method consistency ... WARNING
Error: .First.lib failed for 'CallRFromC'
Call sequence:
2: stop(gettextf(".First.lib failed for '%s'", libraryPkgName(package)),

       domain = NA)
1: library(package, lib.loc = lib.loc, character.only = TRUE, verbose =
FALSE)
Execution halted
See section 'Generic functions and methods' of the 'Writing R
Extensions'
manual.

A little debugging shows that the problem here is caused by
library.dynam as R CMD check runs fine when 
this is exluded.

To be more concrete:

Are both library.dynam and dyn.load necessary?
If they are does it matter in which order they are called?
What different roles do they play in making the compiled functions
available to R?
I notice that many of the packages that use shared objects use
namespaces, is the extra cost of learning
about namespaces justufued by the gains from their use?

Any help or pointers to manuals gratefully received

Phineas Campbell

> version  
         _                   
platform sparc-sun-solaris2.9
arch     sparc               
os       solaris2.9          
system   sparc, solaris2.9   
status                       
major    2                   
minor    1.1                 
year     2005                
month    06                  
day      20                  
language R




More information about the R-help mailing list