How to properly use Fortran shared object in R package - r

I'm writing an R package with the help of devtools.
I have a R function which uses Fortran subroutines (called with .Fortran() function) from a packagename.so file located in the src folder.
I put #useDynLib packagename in R/packagename.R and ran devtools::document() to add useDynLib(packagename) to NAMESPACE but when I run devtools::check() an error occures.
I read the R packages documentation and googled the question but I haven't found a solution yet.
Two related questions come to my mind:
do I need the Fortran source code and let R compile it?
should the shared object have the same name of the package or not?
TL;DR
How do I get rid of the following error after running devtools::check()?
Error: package or namespace load failed for ‘ROCkerMeth’ in library.dynam(lib, package, package.lib):
shared object ‘ROCkerMeth.so’ not found

Related

A package that needs compilation cannot be installed with a different name

I have a github clone of the glue R package for reasons. The name of this package is changed into glue.1.3.1 in this repository from its DESCRIPTION file for similar reasons. As a control group, I have an identical repository but this one's name is not changed in any way
If I do
devtools::install_github('oganm/glue.1.3.1')
the installation will fail
Error: package or namespace load failed for ‘glue.1.3.1’ in library.dynam(lib, package, package.lib):
shared object ‘glue.so’ not found
Error: loading failed
Execution halted
If I do
devtools::install_github('oganm/glue)
the installation will be successful.
If I do the same thing to most other packages, there are no problems but it seems like the source code of pacakges that need compilation, the change in the package name causes problems. For instance, you can repeat the issue with dplyr here
The question is, what causes this issue? What do I need to do so that I can reliably change names of packages that require compilation?

How to connect .so (C code) to a R package

I have a trouble to create a R package.
I have a compiled C code named "a.so", where "void b(...)" and "void c(...)" were defined in a.so. In R code, dyn.load(a.so) works well to use .C("b",...) and .C("c",...).
To create a R package, I saved a.so file to src folder and wrote useDynLib(a) in NAMESPACE file. However, it gave error messages as below. Could you help me how to handle it? I used RStudio on Ubuntu. (I am not sure why library.dynam was shown, since I did not use it).
Error in library.dynam(lib, package, package.lib) :
shared object ‘a.so’ not found
Error: loading failed
Execution halted
ERROR: loading failed

Error building package in R 3.1.2 w/ multiple third party DLL's

A colleague ported more than a thousand S functions and Fortran subroutines to R. The native R functions are contained in 5 .RData files and the Fortran subroutines are contained in 2 .dll files.
I can load these files into the R workspace using
for (i in 1:5){ load(list.files()[grep("RData",strsplit(list.files(),"\\W+"))][i]) }
for (i in 1:2){dyn.load(list.files()[grep("dll" ,strsplit(list.files(),"\\W+"))][i]) }
After some troubleshooting I know that the relationships between R code and the Fortran subroutines were already well established by the original author.
Now I want to create a package from these files because I'm unable to deploy my ioslides_presentations with these dll files since shinyapps.io is built with Linux. This is my first attempt at creating an R package, I don't intend to publish it to the CRAN, I'm only interested in generating a local zip file that I and my students can load.
MY PROCESS
After loading the functions in the workspace using the above code, I ran package.skeleton and system("R CMD build package") which successfully created "package_1.0.tar.gz". After completing the DESCRIPTION file, I put the .dll files I the src sub-directory and included useDynLib(DLL1), useDynLib(DLL2) in the NAMESPACE file.
However, when I run system("R CMD INSTALL build package_1.0.tar.gz") I get the following error:
Error in library.dynam(lib, package, package.lib) :
DLL 'package.dll' not found: maybe not installed for this architecture?
Error: loading failed
Execution halted
I've spent a great deal of time reading through "Writing R Extensions", "Advanced R" by Hadley Wickham, and the devtools documentation. Even so, I'm having difficulty deciphering useDynLib, .onLoad, Export, Call, and Makevars and which should be used for pre-compiled Fortran code.
I really don't want to parse through all of these functions to find which require export rules if I don't have to. I'd appreciate any help getting these .dll files loaded so I can finish creating this package
Thanks in advance,
Jason

install_github install package but I can't call the functions

I can see my package is installed with library(), but when I load the package from the library, I can't call any of the functions.
> install_github("pdfHarvester", "hansthompson")
> library(pdfHarvester)
> Convert
Error: object 'Convert' not found
> Parse_Tables
Error: object 'Convert' not found
You need to make sure you package actually exports the functions you want to use. This is a package development issue - not an issue with install_github.
Note that your NAMESPACE file is empty: https://github.com/hansthompson/pdfHarvester/blob/master/NAMESPACE
It looks like you're using roxygen for your .R files. If you're using devtools for the package creation you need to use document() to generate your help file and populate the NAMESPACE file.

Building an R package with a pre-compiled shared library

I am facing a problem with an R package that I am writing and trying to build with a pre-compiled shared library. Let me try to briefly describe the problem:
this package (let's call it mypack) relies on a shared library mylib.dll that is already compiled and that I cannot compile on the fly while building the R package.
the library mylib.dll has been compiled on a x64 machine under Windows and can be loaded in R with dyn.load.
the package contains the required file NAMESPACE, where useDynLib(mylib.dll) is specified. The function .onLoad containing the instruction library.dynam('mylib.dll', pkg, lib) is also specified in a file zzz.R.
the R package mypack is built with Rtools using the usual command Rcmd INSTALL, and I then add a directory libs where I save mylib.dll.
when I try to load the package in R with library(mypack), I get the following error message:
Error: package 'mypack' is not installed for 'arch=x64'
This is puzzling me. Why can the shared library be loaded smoothly in R, but when I build a package using it I am getting this weird error message?
Thank you very much in advance for your help!
That error message comes from this code in library:
if (nzchar(r_arch) && file.exists(file.path(pkgpath,
"libs")) && !file.exists(file.path(pkgpath, "libs",
r_arch)))
stop(gettextf("package %s is not installed for 'arch=%s'",
sQuote(pkgname), r_arch), call. = FALSE, domain = NA)
which is telling me you need a {package}/libs/{arch} folder in your built package (ie the installed directory) with an {arch} that matches your system's arch, as given by r_arch <- .Platform$r_arch
I'm guessing your build has failed to make this correctly. Is there any C code in your source?

Resources