[Rcpp-devel] Calling C++ Macros in R using Rcpp

Dirk Eddelbuettel edd at debian.org
Sun Aug 28 15:45:57 CEST 2016


Fatima,

You are writing very much like a C programmer. While C and C++ pretty much
get along, we have conflicts here over the macro use --- we make extensive
use of templates, and the cost of that is that some structures (which may be
valid, but really are no longer recommended style) conflict.

That happens here. Let me offer two different solutions.

The first simply splits your macro off into an initializer function and calls
it.  We have

-----------------------------------------------------------------------------
#include <Rcpp.h>

using namespace Rcpp;

NumericVector fillVector(NumericVector v, double amount) {
    for (int i=0; i<v.size(); i++)
        v(i) = amount;
    return v;
}

// [[Rcpp::export]]
double fun(double fillamount, NumericVector array1) {
    array1 = fillVector(array1, 1.1);
    double object = 0.0;
    for (int i=0; i<array1.size(); i++)
        object += array1(i);
    return object;
}
-----------------------------------------------------------------------------

Very straightforward: we pass a vector and an amount, and set the amount in
each element. (But see below.)

We then use that in function fun() to initialize the vector, after which we
loop over it and summarize.

But there is more.

Initializing a vector with a constant in each element is so common that we
have constructor just for that.  And summing each element is so common that
the sugar function sum() does just that.  So now it all is just this
two-liner:

-----------------------------------------------------------------------------
// [[Rcpp::export]]
double simplerfun(double fillamount, int n) {
    NumericVector arr(n, fillamount);
    return sum(arr);
}
-----------------------------------------------------------------------------

When I run this I get:


-----------------------------------------------------------------------------

R> sourceCpp("/tmp/rcppdevel20160828.cpp")

R> fun(1.1, 1:5)
[1] 5.5

R> simplerfun(1.1, 5)
[1] 5.5
R>

-----------------------------------------------------------------------------

As expected.  Hope this helps.  I re-include the whole file below.

Dirk


-----------------------------------------------------------------------------

#include <Rcpp.h>

using namespace Rcpp;

NumericVector fillVector(NumericVector v, double amount) {
    for (int i=0; i<v.size(); i++)
        v(i) = amount;
    return v;
}

// [[Rcpp::export]]
double fun(double fillamount, NumericVector array1) {
    array1 = fillVector(array1, 1.1);
    double object = 0.0;
    for (int i=0; i<array1.size(); i++)
        object += array1(i);
    return object;
}


/*** R
fun(1.1, 1:5)
*/



// [[Rcpp::export]]
double simplerfun(double fillamount, int n) {
    NumericVector arr(n, fillamount);
    return sum(arr);
}

/*** R
simplerfun(1.1, 5)
*/

-----------------------------------------------------------------------------

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list