Reputation: 67
I have encountered a weird problem : when I try to access to an element using brackets( A(1,2) ) R session crashes. When I use A[1,2] it returns the first column and not the second one, and I get the result when I try A[1,3], A[1,4], ...
Here is my code :
#include <Rcpp.h>
#include <String.h>
using namespace Rcpp;
// [[Rcpp::export]]
Rcpp::NumericMatrix Function_bag_ngrams(Rcpp::NumericMatrix idv_x_sacs, Rcpp::StringMatrix idv, Rcpp::StringMatrix liste_ngrammes_gardes, Rcpp::StringVector colNamesIdBags )
{
int nbr_idv = idv_x_sacs.nrow();
int nbr_sac = idv_x_sacs.ncol();
int nbr_composants_sacs = liste_ngrammes_gardes.nrow();
std::string str_aux = "";
std::string idSac = "";
int indexIdSac = 0;
for(int i=0; i<nbr_idv; i++)
{
str_aux = (String) idv(i,1);
for(int j=0; j<nbr_composants_sacs; j++)
{
if(str_aux.find(liste_ngrammes_gardes(j,2)) != std::string::npos)
{
idSac = (String) liste_ngrammes_gardes(j,1);
indexIdSac = 0;
for(int k=0;k<nbr_sac;k++)
{
if((String) colNamesIdBags(k)==idSac)
{
indexIdSac = k;
break;
}
}
idv_x_sacs(i,indexIdSac) = 1;
}
}
}
return idv_x_sacs;
}
I know that the problem comes from the brackets because I tried a simple code trying to access an element and it crashed.
Here is a simple code that I tried :
#include <Rcpp.h>
#include <String.h>
using namespace Rcpp;
// [[Rcpp::export]]
std::string Function(Rcpp::StringMatrix M)
{
std:string tmp = M(1,1)
return tmp;
}
And here is how I use it in R :
sourceCpp("Function.cpp")
Function(matrix(rep("test",4),nrow=2,ncol=2))
Thank's.
Upvotes: 0
Views: 106
Reputation: 368191
You are creating a numeric matrix with
matrix(rep(0,4),nrow=2,ncol=2)
which you are sending into a function expecting a StringMatrix
Function(Rcpp::StringMatrix M)
That cannot possibly work.
Upvotes: 1