How to define a Rcpp package based on several different cpp files? - r

I have define several .cpp files.
One of them needs the package RcppArmadillo;
The others need the package Rcpp.
After my Rcpp package is generated and when i install it, I compile several errors as follows:
RcppExports.cpp:49: error: ‘arma’ has not been declared
RcppExports.cpp:49: error: ‘arma’ has not been declared
RcppExports.cpp:49: error: expected `;' before ‘__result’
RcppExports.cpp:50: error: ‘__result’ was not declared in this scope
make: *** [RcppExports.o] Error 1
When I check RcppExports.cpp file in src, the include head is like this:
#include <Rcpp.h>
What should I do? How to handle this problem? Thank you very much!

This is extensively documented, and there are thirty CRAN packages using RcppArmadillo you could look at for working examples and guidance.
Start with
RcppArmadillo.package.skeleton()
to create an (almost empty) working package for RcppArmadillo, then drop your files in the src/ directory of that package.
The RcppArmadillo.package.skeleton() has options, so consider reading its help page. The Rcpp package has a lot of documentation you may want to look at too, including one entire vignette about package building.

Related

Makevars cannot find libguile.h but the file exists [duplicate]

I have some C++ code that I intend to export into my r package using Rcpp. However, this code links to fftw3 via
#include <fftw3.h>
at the top of the file. When I try to compile this code, I unsurprisingly get the error
fatal error: 'fftw3.h' file not found
What is the proper way to link to this file so that it will be available upon compilation of my package? I know that a Makevars file can generally be used to link to system libraries but since this library is external I'm not sure what to do.
Thanks,
Eric.
Please see the Rcpp vignette Rcpp libraries -- which is also this arXiv paper.
There are many examples among the 2400+ CRAN packages using Rcpp. My hunch would probably be to look at what I contributed to the nloptr package -- even though that is a more complicated scheme where we allow use either a system library if present (could be the case with fftw3 too) or downloand and build.
Rcpp has been used a lot to build such glue. The most common, and simplest , approach is to look for pkg-config and query it for headers and libraries. Please give that a shot (with some looking around CRAN or GitHub for examples).
Edit: There is also an (old) fftw3 package by Gabor at his previous employer's GitHub org as well as another CRAN package fftwtools (which, if memory served, I helped once too but I don't recall now what for).

Linking to fftw3.h in C++ source file for R package

I have some C++ code that I intend to export into my r package using Rcpp. However, this code links to fftw3 via
#include <fftw3.h>
at the top of the file. When I try to compile this code, I unsurprisingly get the error
fatal error: 'fftw3.h' file not found
What is the proper way to link to this file so that it will be available upon compilation of my package? I know that a Makevars file can generally be used to link to system libraries but since this library is external I'm not sure what to do.
Thanks,
Eric.
Please see the Rcpp vignette Rcpp libraries -- which is also this arXiv paper.
There are many examples among the 2400+ CRAN packages using Rcpp. My hunch would probably be to look at what I contributed to the nloptr package -- even though that is a more complicated scheme where we allow use either a system library if present (could be the case with fftw3 too) or downloand and build.
Rcpp has been used a lot to build such glue. The most common, and simplest , approach is to look for pkg-config and query it for headers and libraries. Please give that a shot (with some looking around CRAN or GitHub for examples).
Edit: There is also an (old) fftw3 package by Gabor at his previous employer's GitHub org as well as another CRAN package fftwtools (which, if memory served, I helped once too but I don't recall now what for).

Importing an Rcpp header file in NAMESPACE within an R Package

This is my first package in R, I already have working package but I would remove some rewriting function in cpp file, so I do an header file that work with single function.
How can I put this header in package?
Note that header.h and header.cpp are in src/ directory of package
and the #include "header.h" is in the .cpp file where I use this function
I tried to modify the NAMESPACE file with:
import(myheader)
But, when I do:
R CMD INSTALL mypackage
I receive this error:
Error: package or namespace load failed for 'mypackage' in namespaceExport(ns, exports):
undefined exports: myheader
How can I solve this error?
As #RalfStubner pointed out in the comments, the NAMESPACE file is meant for exporting and importing R functions and data.
The primary requirement for a NAMESPACE files in a package using Rcpp is to ensure:
A single function from Rcpp package is imported for registration reasons.
Generally, either evalCpp or sourceCpp is used.
Provide the name of the shared object via useDynLib(),
This is the name of the R package being built.
importFrom(Rcpp, sourceCpp)
useDynLib(<PACKAGE_NAME_HERE>, .registration = TRUE)
where <PACKAGE_NAME_HERE> is the name of the package without <>.
If you're interested in using headers to share code between R packages, consider looking at:
https://github.com/r-pkg-examples/rcpp-shared-cpp-functions
The main design pattern is using inst/include directory to place a header-only library. Then, in src/ write bindings to the library. Ensure that src/Makevars and src/Makevars.win has:
# Register where the header files for the package can be found
PKG_CXXFLAGS=-I../inst/include/
If you want to share function definitions between .cpp files in the same R package, see:
https://github.com/r-pkg-examples/rcpp-headers-src
This avoids a single monolithic .cpp file, but does not allow for sharing the compiled code routines between R packages outside of the exported R wrapper.

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 BH package not showing "asio" directory

After installing BH package, including following in .cpp file:
#include <Rcpp.h>
#include <boost/asio/ip/address_v4.hpp>
sourcing the cpp file with sourceCpp(".cpp file") is giving :
fatal error: boost/asio/ip/address_v4.hpp: No such file or directory compilation terminated.
In BH/boost directory there is no asio directory.
We never said Boost Asio was part of BH. Quoting from the DESCRIPTION with my highlighting in italics below:
Description: Boost provides free peer-reviewed portable C++ source
libraries. A large part of Boost is provided as C++ template code
which is resolved entirely at compile-time without linking. This
package aims to provide the most useful subset of Boost libraries
for template use among CRAN package. By placing these libraries in
this package, we offer a more efficient distribution system for CRAN
as replication of this code in the sources of other packages is
avoided.
Asio, thread, ... and a few others require linking and can never be part of BH.

Resources