Question

I have been working with the R package "RcppArmadillo". I already used it to define two cxxfunction(they have been debugged are fine to use):

calc1 <- cxxfunction(signature(A="integer", B="integer"),...)
calc2 <- cxxfunction(signature(A="integer", K="integer"),...)

Now I'm writing the body part of another cxxfunction main and wish to call calc1 and calc2 within the for loops there, like:

body_main = '

    ...
    for(int i=0; i<N; i++){
        // This is where I want to call calc1.
        // (?)
        for(int j=0; j<N; j++){
             // This is where I want to call calc2.
             // (?)
        }
    }

'

Is there anyway that I can achieve that? Can that be done in an inline fashion?

I haven't seen an example of inline usage of RcppArmadillo(or Rcpp, RcppGSL) in which people write a subroutine within the body part - specifically, I mean code looks like this:

body_example = '

    // Subroutine
    SEXP(/*or something else*/) func_0(SEXP A, SEXP B){
        ...
        return ...;
    }

    // Then call it from the main part
    ...
    AB = func_0(A, B);
    ...
'

My question probably looks naive but it haunts me nevertheless. Can anyone help explain this? I'd appreciate that a lot!

Was it helpful?

Solution

You could switch from using cxxfunction() from package inline to using Rcpp attributes and its sourceCpp(). That way you get the predictable function headers at the C++ level, see the Rcpp atributes vignette.

Or split calc1 and calc2 into 'worker' and 'wrapper', have cxxfunction() around the wrapper allowing you to call the worker.

The key issue here really is that cxxfunction() exists to create an R-callable function, and it generates internal randomized function headers.

Lastly, a package would help too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top