Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use useDynLib() correctly in an R package namespace file

Although a few solutions exist on the internet, I found none of those suitable for the problem I'm curerntly facing (though maybe I'm simply too dumb):

I'm trying to build an R package which makes extensive use of a shared object compiled by a Makefile (yes, bad practice, I know, but a Makevars file just can't be told to compile C and Fortran code into one shared object) from code in the package's src directory. No matter where I compile that .so to (I tried the src, libs and package base folders) or how I name it (as one of the solutions mentioned above states it must be named like the package it's contained in), R CMD check exits with

** testing if installed package can be loaded
Error in library.dynam(lib, package, package.lib) : 
shared object ‘SoMNibEN.R.so’ not found

due to the useDynLib(SoMNibEN.R) instruction in my NAMESPACE file (where SoMNibEN.R is my package's name, but it didn't work with the original name, either)

My assumption is that I'm either using that useDynLib() command wrong or I'm doing something wrong with my Makefile (although the compilation works pretty well and the shared object is created in my project folder - I just don't know whether it gets copied to the package installation directory successfully).

So, if anyone knows what I might be doing wrong here, please let me know!

like image 416
Sty Avatar asked Oct 19 '12 14:10

Sty


1 Answers

You want the name of the package as the argument, as that is the name of the shared object built by R, eg useDynLib("chron"). The quotes are optional (as they are for library() etc).

I also recommend not using a Makefile, but simply dropping the C and Fortran files in the src/ directory. R is generally smart enough to know what to. If you need -I etc switches you can set them there.

Lastly, use CRAN. There are hundreds of packages with compiled sources, and some are bound to be similar in structure to your question.

like image 103
Dirk Eddelbuettel Avatar answered Oct 17 '22 14:10

Dirk Eddelbuettel