fatal error: 'RcppArmadillo.h' file not found - rcpparmadillo

Rcpp::sourceCpp('~/Desktop/my.cpp')
Was working on some stand alone Rcpparmadillo files and came across this error:
fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>
^~~~~~~~~~~~~~~~~
The example "my.cpp":
// [[Rcpp::depends(RcppArmadillo)]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
arma::vec add_two(arma::vec x){
return x + 2;
}
/*** R
add_two( c(42, 22))
*/
I recently upgraded to RcppArmadillo version 0.9.900.1.0 and I am on macOS Catalina 10.15.5 and R version 3.6.1. This seems unusual as I can still build a package with RcppArmadillo, anyway I found some solution I will post below incase someone else has the same problem.

For others troubleshooting a similar problem, what worked for me was to add these lines to the DESCRIPTION file of my R package:
LinkingTo:
Rcpp,
RcppArmadillo
Posting here in case it's helpful for others. I came across this solution here: https://mattstats.wordpress.com/2016/11/04/r-hub-for-microsoft-r-open/

Copied contents of the folder:
-I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/RcppArmadillo/include"
To:
-I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include"

Related

C++17 in a package with Rcpp

#include <cmath>
#include <math.h>
#include <Rcpp.h>
// [[Rcpp::plugins("cpp17")]]
// [[Rcpp::export]]
double rcpp_hello_world(Rcpp::NumericVector x) {
double z;
z = std::cyl_bessel_i(0, x[0]);
return z ;
}
When I run the above code and call it using sourceCpp, it works as expected. However inside a R package setup I get
error: cyl_bessel_iis not a member of ‘std
11 | z = std::cyl_bessel_i[0]);
| ^~~~~~~~~~~~
make: *** [/usr/lib64/R/etc/Makeconf:177: rcpp_hello_world.o] Error 1
ERROR: compilation failed for package ‘Package’
upon running Rcpp::compileAttributes() and devtools::document(). I initialised the package with Rcpp.package.skeleton.
cyl_bessel_i was added to the standard library in C++17.
I am using gcc-12.1.0-2 and R-4.2.0-3. I am on Arch Linux.
The default C++ standard used in current versions of R is C++11 (or C++14 if available). Since you require C++17, you need to declare it in the src/Makevars file, using the line
CXX_STD=CXX17
In a comment you referred to the Rcpp documentation which says that Makevars is optional since Rcpp 0.11.0. I think that was written about linking to the Rcpp libs, but it is also true here, as pointed out by #MikkoMarttila: you can alternatively declare the C++ version in the SystemRequirements: field of the DESCRIPTION file, e.g.
SystemRequirements: C++17

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!
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.

Combine R, C++ and Fortran

I am trying to reimplement an R function using C++ and RCpp to speed up the computation. And in the C++ implementation, I need to use a Fortran function mvtdst found in link.
#include <Rcpp.h>
#include "mvtnorm.h"
using namespace Rcpp;
// [[Rcpp::export]]
NumericVector pmvnorm_rcpp(NumericVector upper, NumericMatrix corr)
{
double error;
double mvnP = pmvnorm_P(2, upper, corr, &error) ;
return mvnP ;
}
/*** R
pmvnorm_rcpp(c(1.5,1.5),c(0.0))
*/
Here, pmvnorm_P is defined in the mvtnorm.cpp file.
All the files found in link are kept in the working directory along with the RcppWrapper.cpp file.
When I compile my RcppWrapper.cpp file using sourceCpp() function in RCpp package, it gives the following error.
mvtnorm.o:mvtnorm.cpp:(.text+0x7c): undefined reference to `mvtdst_'
collect2.exe: error: ld returned 1 exit status
Error in Rcpp::sourceCpp("RcppWrapper.cpp") :
Error occurred building shared library.
Does anyone know how to resolve this error?
When you have code in two sources files
mvtnorm.cpp calling your backend function pmvnorm_P()
another file providing it
then you also must provide link instructions. Simply put, sourceCpp() is only intentended and working for one-file solutions (unless you give link instructions).
Simplest fix: just create a package assembling all your files in src/.
Fortran and C++ mangle the names of functions differently. It looks like mvtdst is the name of your Fotran function. (Right?) You need to "mangle" that by hand when called from C++. So instead of calling mvtdst, call mvtdst_ with the trailing underscore.
Unfortunately, compilers are not consistent in the mangling, so this will not be portable. (To make it portable, you'll need some sort of preprocessing that matched the mangling to the compiler.)

R CMD check note: Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’

How to avoid the following NOTE that is appearing in R CMD check with the new R development version ( R Under development (unstable) (2017-02-15 r72179))?
• checking for unstated dependencies in examples ... OK
• checking line endings in C/C++/Fortran sources/headers ... OK
• checking compiled code ... NOTE
File ‘pkgname/libs/pkgname.so’:
Found no calls to: ‘R_registerRoutines’, ‘R_useDynamicSymbols’
It is good practice to register native routines and to disable symbol
search.
For example in Hmisc
The message is somewhat arcane. I looked around also in other packages and I found that the useDynLib(packagename) in the NAMESPACE file was replaced by useDynLib(packagename, .registration = TRUE).
In addition, I added a .c file, named registerDynamicSymbol in the src/ directory with the following code:
// RegisteringDynamic Symbols
#include <R.h>
#include <Rinternals.h>
#include <R_ext/Rdynload.h>
void R_init_markovchain(DllInfo* info) {
R_registerRoutines(info, NULL, NULL, NULL, NULL);
R_useDynamicSymbols(info, TRUE);
}
I took this suggestion from GitHub Rcpp. The canonical reference is in Writing R Extensions
Also R Devel Mailinglist provided supplementary infos.
UPDATE
The most direct straightforward approach is:
use the current R Development Version (that will eventually become 3.4)
Run the tools::package_native_routine_registration_skeleton(".") and copy and paste the full output in a packagename_init.c file to be put in src/
update NAMESPACE, verifying that useDynLib(packagename, .registration = TRUE)
If necessary, replace the exportPattern with export( list of object to be exported )
UPDATE 18th July
As noted by #Symbolix using the most recent version of R and RStudio's devtools the point 2. (init.c files) appears handled by either devtools (using RStudio check digit) or tools packages.
I ran into a persistent issue with a Windows build package. (.dll instead of .so)
The accepted answer above should also resolve this issue for Windows, but if it does not resolve it. Make sure that objdump.exe is pointing the appropriate arch. i.e. .../Mingw_64/bin/objdump.exe. This can be checked from a command prompt with which objdump.exe. Somehow a 32-bit objdump.exe found its way into a higher priority position in my path. This arch mismatch will produce a File format not recognized error.
First I did exactly what Giorgio Spedicato says. But still got NOTE warnings. Finally I got problem solved by doing this:
Sys.setenv(PATH = paste(Sys.getenv("PATH"),
"C:\RTools40",
"C:\RTools40\mingw64\bin",
sep = ";"))
Have to add mingw64\bin to PATH because that's where objdump.exe is located

Rcpp: "Cannot change working directory" when called with embedded R code

I have been successfully using Rcpp for a while, and have been able to experiment with most of its features. However, on a Windows 7 machine, with RStudio 0.98.1049 I am not able to sourceCpp a file that has embedded R code chunks in it.
Here is M(N)WE:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double plusOne(double x) {
return x + 1.0;
}
/*** R
plusOne(3)
*/
When I sourceCpp(..., embeddedR = TRUE), I get this error message:
Error in setwd(rWorkingDir) : cannot change working directory
which I am guessing arises from line 181 here. Not sure how to fix this. I am able to change directories from RStudio in general.
This issue was resolved with Rcpp 0.11.3 -- it should go away if you update.

Resources