问题
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
回答1:
The issue here is that CharacterVector
and CharacterMatrix
types are really just vectors of SEXP
s -- more specifically, a CharacterVector
wraps a STRSXP
, whose contents is an array of SEXP
s (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 CHARSXP
s 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.
来源:https://stackoverflow.com/questions/29105531/rcppparallel-converting-charactermatrix-to-rmatrixt