2

Hello Dear Community,

I am learning RcppParallel and have this issue while trying to convert a Rcpp::CharacterMatrix to RcppParallel::RMatrix using following codes:

struct CharMatDist : RcppParallel::Worker {
    const RcppParallel::RMatrix<std::string> A; 
    const RcppParallel::RMatrix<std::string> B; 
    const NumericVector w; 
    RcppParallel::RMatrix<double> ret; 
    int n; 

    CharMatDist(CharacterMatrix A, CharacterMatrix B, NumericVector w, NumericMatrix ret)
    : A(A), B(B), w(w), ret(ret) {
        n = B.nrow();
    }

...

}

As the gallery suggests, I would expect the convertion to be carried out automatically in A(A) etc. but it is not working and gives me following error:

cannot convert 'Rcpp:Matrix<16>::iterator (aka Rcpp::internal::Proxy_iterator<Rcpp::internal::string_proxy<16> >)' to 'char*' in initialization

I also tried

RMatrix<char>

and even manual cast in body but also without success. Where am I doing wrong??

Any suggestions and help would be highly appreciated.

YYA

Tuyki
  • 21
  • 2
  • Can you please include the rest of your code (the `...`)? – nrussell Mar 17 '15 at 18:20
  • The rest should kinda calculate some distance measures between each pair of rows in A and B respectively, on which I am still working. According to my tests, the existing codes are already enough to produce the error prefectly. ;) – Tuyki Mar 17 '15 at 18:27

1 Answers1

2

The issue here is that CharacterVector and CharacterMatrix types are really just vectors of SEXPs -- more specifically, a CharacterVector wraps a STRSXP, whose contents is an array of SEXPs (of runtime type CHARSXP). Unfortunately, knowing these details is necessary when interacting with R's character types.

I have no idea if this will work, but if it did, you'd want to use something like an RMatrix<SEXP>, which would (presumedly) map over the underlying CHARSXPs inside the matrix -- wherein you could access the underlining const char* string with CHAR.

The RMatrix class is not 'smart' enough to directly provide access to the underlying strings in this case, unfortunately, so I believe you will have to unwrap it yourself.

Kevin Ushey
  • 20,530
  • 5
  • 56
  • 88
  • Thanks Kevin. After trying out your suggestion and more possibilities like accessing RMatrix with iterators(, which actually works), I still cannot convert one from CharacterVector or -Matrix. So I have decided simply to code the characters in numericals. Maybe we could hope for an improvement in the updates to come. :D – Tuyki Mar 19 '15 at 12:16
  • 1
    An important note from the [Rcpp Gallery](http://gallery.rcpp.org/articles/parallel-matrix-transform/) > Note also that we use the RMatrix type for accessing the matrix. This is because this code will execute on a background thread where it’s not safe to call R or Rcpp APIs. The RMatrix class is included in the RcppParallel package and provides a lightweight, thread-safe wrapper around R matrixes. – Peter Apr 15 '17 at 08:28