RcppParallel: Converting CharacterMatrix to RMatrix<T>

余生颓废 提交于 2019-12-11 03:49:08

问题


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 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.



来源:https://stackoverflow.com/questions/29105531/rcppparallel-converting-charactermatrix-to-rmatrixt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!