I want to build an Rcpp package with C++ and R code. With the C++ code only (R code removed), everything compiles and works nicely and after building and loading the exported functions are callable as expected as
<packageName>::<functionName>()
However, when including the R code with the call to the C++ code, I get an object not found error when building the package:
R CMD build <packageName>
[...]
** R
** byte-compile and prepare package for lazy loading
Error in <functionName>() :
object '_<packageName>_<functionName>' not found
Error: unable to load R code in package '<packageName>'
The error happens after the C++ code is successfully compiled.
<functionName>()
is mapped to
_<packageName>_<functionName>
in R/RcppExports.R as usual, but it does not appear that it can be loaded by the R code at build time.
The structure of the package is as usual:
<packageName>
├── DESCRIPTION
├── man
│ ├── <functionName>.rd
│ └── <packageName>-package.rd
├── NAMESPACE
├── R
│ ├── <RCodeFileName>.R
│ └── RcppExports.R
├── README.md
└── src
├── <C++CodeFileName>.cpp
├── <C++CodeFileName>.o
├── <packageName>.so
├── RCppExports.cpp
├── RCppExports.o
└── symbols.rds
where the .o and .so files are produced when compiling the package.
The R file calls the function from the C++ file directly:
[other stuff]
[...]
<functionName>()
NAMESPACE file is also as usual:
useDynLib(<packageName>, .registration=TRUE)
importFrom(Rcpp, evalCpp)
exportPattern("^[[:alpha:]]+")
This seems like a very simple and straight forward problem (calling C++ code from R code in an Rcpp package). Nevertheless, I seem to be unable to find any indication of how this should be done.
Edit (Apr 8 2020): build vs. INSTALL
The problem will occur in every case for
R CMD INSTALL <packageName>_<version>.tar.gz
If the included help file
man/<packageName>-package.Rd
is built so that it forces installing the package to process help pages the error will already occur in
R CMD build <packageName>
as described above.
Edit (Apr 8 2020): Steps to reproduce with Rcpp.package.skeleton() (on Unix-like systems anyway)
Rscript -e 'Rcpp::Rcpp.package.skeleton("demo20200408")'
echo 'rcpp_hello_world()' > errorDemo/R/example.R
R CMD INSTALL errorDemo