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

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

Related

"object not found" during package build/install

I'm at a loss to debug building/installing a package. Building a package "MyProjekt" through
> devtools::build("MyProjekt")
works fine, even though
> devtools::document()
Updating MyProjekt documentation
Loading MyProjekt
Error: object 'rank' not found whilst loading namespace 'MyProjekt'
fails.
Attempting to install the built (pure R) package
> install.packages(pkgs="./MyProjekt.tar.gz")
...
Error: package or namespace load failed for 'MyProjekt':
object 'rank' not found whilst loading namespace 'MyProjekt'
Error: loading failed
Execution halted
...
ERROR: loading failed for 'i386', 'x64'
also fails.
Unfortunately the term 'rank' is a common term in my domain and I have hundreds of hits when searching in my files for that term.
How might I start a systematic way to hunt this bug down?
The toolchain mistakenly generates S3method(...) in the NAMESPACE file.
Follow this answer and edit the NAMESPACE file by hand. Replace occurrences of S3method(f) with export(f).
I'm not certain this is repeatable. The generated files appear to be not just written, but also read during a subsequent package build (by devtools? by roxygen2?). Add your comments if this works/does not work for you.

How to properly use Fortran shared object in R package

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

R - trouble loading "forecast" package

When trying to load the "forecast" package in R using
library(forecast)
I get the pop-up error message: "The procedure entry point IdnToAscii could not be located in the dynamic link library KERNEL32.dll".
This is then followed by an error message in the console: "unable to load shared object 'C:/Documents and Settings/hq/My Documents/R/win-library/3.3/curl/libs/i386/curl.dll':
LoadLibrary failure: The specified procedure could not be found."
From the research I've done on this, it looks as though this problem is occurring because R is having difficulty with that specific library location. So I've re-installed "forecast", using a different location:
library(forecast,lib.loc="C:/Documents and Settings/hq/Desktop/RDirectory/New Library")
I have checked, and the files are now in that second location. However, when I do:
library(forecast,lib.loc="C:/Documents and Settings/hq/Desktop/RDirectory/New Library")
I get the same pop-up error message, followed by the same error message in the console: "Error in inDL(x, as.logical(local), as.logical(now), ...) :
unable to load shared object 'C:/Documents and Settings/hq/My Documents/R/win-library/3.3/curl/libs/i386/curl.dll':". This suggests that R is ignoring my instruction to look in a different library location, and going back to the same location.
Apologies - I'm sure this is a very basic question, but I've never used R before.
Update - I wanted to check that this wasn't a generic problem with downloading packages from R, so I have installed another package (e1071) at random and run a random function from that package (the "naiveBayes" function), and this installed, loaded and ran perfectly. This suggests that this is perhaps something to do with the installation of this specific package.
Update 2 - just changed the library location. Made absolutely no difference. I am using Windows XP and R version 3.3.0, if either of these is relevant...

libstableR doesn't build for macOS

I would like to use the R package libstableR on a mac. I'm not a contributor/author of the package and have contacted them regarding libstableR failing for osx:
After contacting the authors who said the problem is probably related to R not knowing where GSL is on the computer, I applied two minor edits to
the DESCRIPTION and the src/Makevars in addition to requiring the mac user to do the following:
be sure to have homebrew
be sure to have gsl
be sure to have Rcpp and RcppGSL: just install.packages("RcppGSL") in R
Sources:
Dirk Eddelbuettel answer on SO
GREGGJ website on homebrew installing GSL on macs
Applying the steps above has changed the output of
devtools::install_github("swihart/libstableR")
From:
<SNIP>
** testing if installed package can be loaded
Error: package or namespace load failed for ‘libstableR’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/swihartbj/RLIB/libstableR/libs/libstableR.so':
dlopen(/Users/swihartbj/RLIB/libstableR/libs/libstableR.so, 6): Symbol not found: _stable_set_THREADS
Referenced from: /Users/swihartbj/RLIB/libstableR/libs/libstableR.so
Expected in: flat namespace
in /Users/swihartbj/RLIB/libstableR/libs/libstableR.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Users/swihartbj/RLIB/libstableR’
to:
** testing if installed package can be loaded
Error: package or namespace load failed for ‘libstableR’ in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object '/Users/swihartbj/RLIB/libstableR/libs/libstableR.so':
dlopen(/Users/swihartbj/RLIB/libstableR/libs/libstableR.so, 6): Symbol not found: _libstableR_stable_cdf
Referenced from: /Users/swihartbj/RLIB/libstableR/libs/libstableR.so
Expected in: flat namespace
in /Users/swihartbj/RLIB/libstableR/libs/libstableR.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Users/swihartbj/RLIB/libstableR’
Where basically instead of complaining about _stable_set_THREADS the errors pertain to _libstableR_stable_cdf. libstableR_stable_cdf is an exported function. The leading underscore in _libstableR_stable_cdf is put there when Roxygen/Rcpp is applied.
I have tinkered and online searched for about two months trying to get this up and running, and now come to SO for help.
Anyway to get libstableR working on a mac?
So, I went to https://cran.rstudio.com/web/packages/libstableR/index.html and grabbed https://cran.rstudio.com/src/contrib/libstableR_1.0.tar.gz (I knew Bruce had a repo but I wanted to see the original).
I had gsl (homebrew) and the RcppGSL installed already.
I did a quick devtools::document() since I figured it used Rcpp everywhere and got:
Error in dyn.load(dllfile) :
unable to load shared object '/private/tmp/libstableR/src/libstableR.so':
dlopen(/private/tmp/libstableR/src/libstableR.so, 6): Symbol not found: _libstableR_stable_cdf
Referenced from: /private/tmp/libstableR/src/libstableR.so
Expected in: flat namespace
in /private/tmp/libstableR/src/libstableR.so
There were two definitions (one Rcpp exposed and one internal) for stable_cdf() so I made the internal one int_stable_cdf() and made the change to the singular call to it.
I still got a similar error so I suspected they hand-crafted some things.
I went in and made the NAMESPACE auto-generate from roxygen comments and added //' #exports for the key exposed functions.
I removed init.c since Rcpp auto-gen's registration code now.
I still got an error, but this time for stable_set_THREADS being unavailable.
It turns out they really don't care abt macOS folks at all:
#ifdef __WIN32
void stable_set_THREADS(unsigned int value) {
/*
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
THREADS = (unsigned int)sysinfo.dwNumberOfProcessors;
*/
THREADS = 12;
}
#else
#ifdef __unix
void stable_set_THREADS(unsigned int value) {
if (value == 0) THREADS = sysconf(_SC_NPROCESSORS_ONLN);
else THREADS = value;
//printf("\nCPUs = %u\n",THREADS);
}
#endif
#endif
I removed the unix ifdef, tweaked a few more incidentals and it works.
I'll see if we can get this on GH and get the maintainers to OK some further tweaks to make it easier for others to contribute.

working with package without Namespace in R

I have got betaversion from website. The only available is windows installation as mybetapackage.zip file. When I installed the package, it does not work when I load it.
> utils:::menuInstallLocal()
> require(mypackage)
Loading required package: mypackage
Failed with error: ‘package ‘mypackage’ does not have a NAMESPACE and should be re-installed’
Is it possible to use this beta package someway ?
Edits:
I tried the following instructions to create own namespace. Namespace file contains the following:
exportPattern("^[^\\.]")
Now I am getting following warning.
Warning message:
In readLines(file) :
incomplete final line found on 'C:/Users/user/Documents/R/win-library/2.14/RCropStat/NAMESPACE
Based on the discussion above I am answering my own questions. All credit goes to who discussed this issue.
Create a file with the following text and put an extra line to avoid an error. Same in the directory for the package where you have description file.
exportPattern("^[^\\.]")

Resources