Search code examples
rrcppdevtoolsroxygen2

fatal error: RcppArmadilloExtensions/sample.h: No such file or directory


I will apologize in advance for the lack of a reproducible example (yet) - if this isn't an easy answer without that, I'll work on one tomorrow (although due to the nature of the question, I'm not sure how easy that will be).

First, I've got my first Rcpp code project working! It's amazingly fast, and does exactly what I needed it to do! Thanks to everyone who helped me here, it was appreciated.

My next task is to add this to a package. I've been using R packages by Hadley Wickham to put together my package, so I'm using roxygen2 and devtools::document() for documentation and general checks, following that book. I turned to the chapter on compiled code (http://r-pkgs.had.co.nz/src.html, for easy reference), and implemented those steps. Specifically:

  • I ran devtools::use_rcpp() to set up the package to use Rcpp.
  • I added the appropriate lines to another function in the package.
  • I copied the file with my function in into the new src directory.

Then I tried to update the documentation (devtools::document()), and I got the following error:

simulate_mean.cpp:2:44: fatal error: RcppArmadilloExtensions/sample.h: No such file or directory
 #include <RcppArmadilloExtensions/sample.h>
                                            ^

I 've been scouring stackexchange for anything like this, and saw an old post where one of the comments was the need for // [[Rcpp::depends(RcppArmadillo)]] at the top (Rcpp R sample equivalent from a NumericVector). That is not the issue for me - that line is in the correct place, with no extra spaces (another issue I found on stackexchange).

The first few lines of the file look like this:

#include <RcppArmadilloExtensions/sample.h>
// [[Rcpp::depends(RcppArmadillo)]]

using namespace Rcpp;

(nothing wrong that I can see, and it compiles fine outside of the package).

My description file specifically imports both Rcpp and RcppArmadillo. Here's the relevant section of that:

Imports:
    dplyr(>= 0.7.4),
    purrr (>= 0.2.4),
    Rcpp (>= 0.12.17),
    RcppArmadillo (>= 0.8.600.0.0)
Suggests: mvtnorm (>= 1.0-6),
    testthat
Encoding: UTF-8
LazyData: true
RoxygenNote: 6.0.1
License: MIT + file LICENSE
LinkingTo: Rcpp

I've updated R and all packages as part of my troubleshooting, so everything is using the latest version. (I also tried the github version of devtools, with the same result). Any thoughts on why the sample.h extension cannot be found using document()?

Thanks in advance!


Solution

  • In your C++ file you have

     // [[Rcpp::depends(RcppArmadillo)]]
    

    For Rcpp::sourceCpp() this sets up the necessary compiler flags for the header files for RcppArmadillo to be found. This does not have the same effect in a package. In order to have this in a package, you will need to include RcppArmadillo in LinkingTo in your DESCRIPTION:

    LinkingTo: Rcpp, RcppArmadillo
    

    BTW, an easy way to get the structure right is to use

    RcppArmadillo::RcppArmadillo.package.skeleton()
    

    for setting up the package skeleton.