3

I followed all the procedures explained so far about this matter either in this website or published notes by Dirk, Hadley or others. However, I still have problems in building my package due to the issue regarding cpp11 plugin.

I used RcppArmadillo.package.skeleton() function. I put my cpp file in the src directory. The NAMESPACE file looks as it should which contains importFrom(Rcpp, sourceCpp) line. I also edited DESCRIPTION file and in the LinkingTo section, I added RcppEigen and other packages I use. I finally ran the compileAttributes(verbose=TRUE) function in R and everything looked OK. Therefore, I think I have done everything as I should. I have to also mention that when I compile my code in R using sourceCpp(), it works perfect and is compiled with no errors! To illustrate better what my dependencies are, I put the first block of my code here:

    #include <RcppArmadillo.h>
    #include <RcppNumerical.h>
    #include <RcppArmadilloExtensions/sample.h>
    #include <Eigen/LU> 
    #include <algorithm>

    // [[Rcpp::depends(RcppArmadillo)]]
    // [[Rcpp::depends(RcppEigen)]]
    // [[Rcpp::depends(RcppNumerical)]]
    // [[Rcpp::plugins(cpp11)]]

The problem is when I build my package and I get errors and warnings for the lines I have auto type which relates to cpp11 plugin.

After searching similar posts on this website, I concluded that I have to force my R compiler to use c++11 and there fore I edited my Makvars file located at ~/.R/Makevars and since I use MAC I added this line: CXX=clang++ -std=c++11 to that file. However, when I do that those 3 errors go away but 50 new errors are generated as all of the Armadillo variable types, such as mat, uvec, etc are not recognized any more. So I don't know how to fix this.

I think basically putting // [[Rcpp::plugins(cpp11)]] should take care of it as the new version of Rcpp supports this plug in and probably that's why when I run sourceCpp in R I get no errors and everything looks fine. But I don't know what happens when building my package. My Rcpp version is 0.12.8 . Thank you in advance for any sorts of help.

Amir Nik
  • 65
  • 8

1 Answers1

2

Plugins for both dependencies (ie other headers) and compiler options are for use by sourceCpp().

Packages do this with LinkingTo: and, for the C++11 directive, either src/Makevars or SystemRequirements. See Writing R Extensions which documents this.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Thank you Dirk for your response. Following your answer, I read [this part](https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Using-C_002b_002b11-code) of the Writing R Extensions about C++11 and added one line to the `src/Makevars` file. But the problem still exists that `Armadillo` functions are not recognized! Does it mean `RcppArmadillo` and `C++11` are not compatible? – Amir Nik Feb 08 '17 at 20:21
  • Keep reading the same manual, looking for `LinkingTo`. Use the **documented** function `RcppArmadillo.package.skeleton()` to create a **working** package and figure our where your attempt is falling short. – Dirk Eddelbuettel Feb 08 '17 at 20:50
  • Dirk, I was finally able to fix the problem! I could eventually find it by comparing my package to a test package by running `RcppArmadillo.package.skeleton()`. The reason was that I use `using namespace arma` in the beginning of my code and never use `arma::` before using Armadillo types. The error was produced in `RcppExports.cpp` file where the `using namespace arma` was missing. I added that line myself and it fixed the problem. However, it's mentioned there "do not edit by hand". Is there anyway that the line is added automatically? Does editing by hand cause problems? – Amir Nik Feb 08 '17 at 22:21
  • Dirk, I also wanted to use this opportunity and directly thank you for making Rcpp and all of the related packages. They are extremely useful! You are great and I really appreciate your efforts in general and specially for the R community. – Amir Nik Feb 09 '17 at 00:27
  • I think I have to write this as an update. As an answer to my question if editing by hand can cause problem, I should say Yes. Because when I used the package made after I added `using namespace arma` by hand to the `RcppExport.cpp` in cluster for parallel computing, it did not work and produced: `Null value passed`! As a result, I removed that line in my source code, added `arma::` before all `Armadillo` objects, and re-built the package. No error was produced in building the package and it works perfectly in the cluster as well! – Amir Nik Feb 09 '17 at 05:50