[Rcpp-devel] conflict between R_ext/Applic.h and RcppArmadillo.h?

Dirk Eddelbuettel edd at debian.org
Thu Mar 1 22:36:02 CET 2012


Hi Richard,

On 1 March 2012 at 16:25, Richard Chandler wrote:
| Hi,
| 
| I am trying to call Rdqags (from R_ext/Applic.h) in a C++ program that
| uses RcppArmadillo. Everything seems to work fine except the compiler
| warns about conflicts. Is there a way to avoid these conflicts? Could
| they cause any unforeseen problems? Below is a self-contained example
| illustrating the problem. Note that RcppArmadillo is not actually
| required in the example, but it is in my program. See below for
| sessionInfo().

Hm, that's interesting.   I haven't used R_ext/Applic.h much; it says

    /* This header file contains routines which are in the R API and ones which
   are not.

   Those which are not can be used only at the user's risk and may change
   or disappear in a future release of R.
   */

so in a way that gives access from C to the R API.  Not surprising that it
bites with what we have.

Rcpp and RcppArmadillo have a moderately involved hierarchy of headers, we
generally do not recommend mixing or changing.

So the simplest suggestion I have ... is to 'split' things. Have your
RcppArmadillo code, have it call a C function ... defined over in another
source file and header which just includes R_ext/Applic.h and communicates
with the other side via standard C (or C++ / STL types).  Makes sense?

Let us know how that goes.  

Dirk


| Thanks,
| Richard
| 
| 
| library(RcppArmadillo)
| library(inline)
| 
| # Integrand
| inc <- "
| #include <R_ext/Applic.h>
| void f(double *x, int n, void *ex) {
|   double *v;
|   v = (double*)ex;
|   double sigma = v[0];
|   for(int i=0; i<n; i++) {
|     x[i] = exp(-(x[i]*x[i]) / (2*sigma*sigma));
|   }
| }
| "
| 
| # Compute integral
| f <- cxxfunction(signature(sigma_="numeric",
|                            lower_="numeric", upper_="numeric"),
|   "
|     double sigma = as<double>(sigma_);
|     double lower = as<double>(lower_);
|     double upper = as<double>(upper_);
|     void *ex;
|     ex = σ
|     double epsabs = 0.001;
|     double epsrel = 0.001;
|     double result = 0.0;
|     double abserr = 0.0;
|     int neval = 0;
|     int ier = 0;
|     int limit = 50;
|     int lenw = 200;
|     int last = 0;
|     int iwork = 50;
|     double work = 200.0;
|     Rdqags(f, ex, &lower, &upper, &epsabs, &epsrel, &result, &abserr,
|            &neval, &ier, &limit, &lenw, &last, &iwork, &work);
|     return Rcpp::List::create(result, abserr, last, ier);
|   ", plugin="RcppArmadillo", includes=inc)
| 
| fr <- function(x, sigma=1) exp(-x^2 / (2*sigma*sigma))
| 
| sig <- 10
| f(sig, 0, 2)
| str(integrate(fr, 0, 2, sigma=sig))
| 
| 
| 
| 
| 
| > sessionInfo()
| R version 2.14.1 (2011-12-22)
| Platform: i386-pc-mingw32/i386 (32-bit)
| 
| locale:
| [1] LC_COLLATE=English_United States.1252
| [2] LC_CTYPE=English_United States.1252
| [3] LC_MONETARY=English_United States.1252
| [4] LC_NUMERIC=C
| [5] LC_TIME=English_United States.1252
| 
| attached base packages:
| [1] stats     graphics  grDevices utils     datasets  methods   base
| 
| other attached packages:
| [1] inline_0.3.8         unmarked_0.9-6       RcppArmadillo_0.2.34
| [4] Rcpp_0.9.9           lattice_0.20-0       reshape_0.8.4
| [7] plyr_1.7.1
| 
| loaded via a namespace (and not attached):
| [1] grid_2.14.1  tools_2.14.1
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list