I have a C package which builds a executable with several argument flags. One compiles the code with a Makefile (I know, this needs to change for the R package) and an executable is created to be run via
$ ./codeName -f path/inputfile -o path/outputfile -p ## -s "type"
My goal is to integrate several of the functions used in this C package to be used with an R library. I take a look at some examples on github.com/cran
of R packages using C. In Writing R Extensions, it explains how I could use .Call()
and Makevars
to call the C functions from R. I would like to avoid that like the plague. However, it looks like this would require significant re-writing with SEXP
object--so I turn to Rcpp (yeah!)
I create the package Rcpp.package.skeleton("packageName")
Great. Within R, I do the following:
$ R
> library(devtools)
> build() # works!
> install() # works!
> library(packageName)
> rcpp_hello_world()
## expected output
Everything works. Then I add my C package into /src
. I then execute Rcpp::compileAttributes()
in the package root directory via R--nothing happens and nothing is output, which is expected, as I haven't changed the C code at all yet.
I try installing with the commands above: devtools::build()
and devtools::install()
. Via the Makefile
, it looks like the C code compiles perfectly! But then there's this issue:
** R
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in library.dynam(lib, package, package.lib) :
shared object ‘packageName.so’ not found
Error: loading failed
Execution halted'
ERROR: loading failed
Well, that's somewhat confusing, and I don't know why that has occurred, but the snag is the useDynLib("packageName")
in the NAMESPACE. If I remove this, the C code appears to compile and the package installs via the build/install
commands above. rcpp_hello_world()
still works.
(1) Why does this error ‘packageName.so’ not found
appear now, and can I get around it?
(This question has nothing to do with Rcpp.)
Then, I go to a .c
file. I add
#include <Rcpp.h>
using namespace Rcpp;
to a *.c
file and //[[Rcpp::export]]
before a function I would like to import. (I'm not sure that's going to work in *.c
, or in a C header file.)
Next, I go to the package root directory, open R and try this:
$ R
> library(Rcpp)
> compileAttributes()
That runs without error. However, no RcppExports.R
and RcppExports.cpp
were generated. Compiling the C code also results in the error that it cannot find #include <Rcpp.h>
.
(2) Why would compileAttributes()
not function in this environment? I must be incorrectly using Rcpp
and //[[Rcpp::export]]
in order to wrap these C functions into R-usable format.