[Rcpp-devel] Rcpp function variable which is given as argument

Mario Deng mariodeng at googlemail.com
Wed Jul 9 12:21:29 CEST 2014


Hello everyone,

I am observing a, to me, strange behaviour. Rcpp seems to modify my variable, which is given as argument. Here is my code:

In R am am running:

Rcpp::sourceCpp("~/.../VectorList/VectorList.cpp")
foo=matrix(runif(52), ncol=4)
colnames(foo)=paste("X",1:ncol(foo),sep="")
rownames(foo)=paste("Y",1:nrow(foo),sep="")

Until here foo looks like:
> foo
           X1        X2        X3         X4
Y1  0.2786725 0.4024807 0.4968645 0.80620023
Y2  0.6441421 0.3540515 0.7312126 0.03784105
Y3  0.9090161 0.4328849 0.6004133 0.69923867
Y4  0.2553254 0.5678732 0.7917617 0.25911206
Y5  0.2667057 0.3320151 0.7481575 0.90253673
Y6  0.2635481 0.4849927 0.9537019 0.73662740
Y7  0.4501414 0.9451928 0.8241006 0.54619146
Y8  0.9177439 0.9556434 0.4024955 0.05961032
Y9  0.3763357 0.7825898 0.1443202 0.27215527
Y10 0.5892217 0.6663081 0.3577415 0.69333984
Y11 0.9915392 0.4184379 0.6896782 0.22941638
Y12 0.5609363 0.6079867 0.7183952 0.24935215
Y13 0.8792352 0.8951102 0.4679093 0.19242862

After running createVectorList(foo) (which is a Rcpp function) foo looks like (it's sorted):

> foo
           X1        X2        X3         X4
Y1  0.2553254 0.3320151 0.1443202 0.03784105
Y2  0.2635481 0.3540515 0.3577415 0.05961032
Y3  0.2667057 0.4024807 0.4024955 0.19242862
Y4  0.2786725 0.4184379 0.4679093 0.22941638
Y5  0.3763357 0.4328849 0.4968645 0.24935215
Y6  0.4501414 0.4849927 0.6004133 0.25911206
Y7  0.5609363 0.5678732 0.6896782 0.27215527
Y8  0.5892217 0.6079867 0.7183952 0.54619146
Y9  0.6441421 0.6663081 0.7312126 0.69333984
Y10 0.8792352 0.7825898 0.7481575 0.69923867
Y11 0.9090161 0.8951102 0.7917617 0.73662740
Y12 0.9177439 0.9451928 0.8241006 0.80620023
Y13 0.9915392 0.9556434 0.9537019 0.90253673

I don't perform any allocation for foo. Why is this Rcpp function (shown below) modifying foo?
Here is the code I am behind createVectorList()

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
List createVectorList(NumericMatrix df) {
  CharacterVector rownames = VECTOR_ELT(df.attr("dimnames"), 0);
  CharacterVector colnames = VECTOR_ELT(df.attr("dimnames"), 1);
  // Check Dimnames
  if (df.nrow() != rownames.size() ) { 
    throw Rcpp::exception("Dimensions and corrosponding names differ in length");
  }
  List vectorList(df.ncol());
  for(int i = 0; i < df.ncol(); i++){
    NumericVector sorted_vec = df(_,i);
    sorted_vec=sorted_vec.sort();
    NumericVector one_col = df(_,i);
    df(_,i)=sorted_vec;
    IntegerVector matched;
    matched = match(sorted_vec, one_col);
    sorted_vec.attr("names")=rownames[matched-1];
    vectorList[i]=sorted_vec;
  }
  vectorList.attr("names") = colnames;
  CharacterVector classIDs = CharacterVector::create("VectorList", "list");
  vectorList.attr("class") = classIDs;
  return vectorList;
}


Is there anything I am missing? I mean, how can Rcpp even access my variables?!

With all the best,

Mario
__
MD
mariodeng at googlemail.com






More information about the Rcpp-devel mailing list