RcppParallel:将 CharacterMatrix 转换为 RMatrix<T> [英] RcppParallel: Converting CharacterMatrix to RMatrix<T>
问题描述
亲爱的社区,您好,
我正在学习 RcppParallel 并在尝试使用以下代码将 Rcpp::CharacterMatrix 转换为 RcppParallel::RMatrix 时遇到此问题:
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();
}
...
}
正如画廊所建议的,我希望在 A(A) 等中自动进行转换,但它不起作用并给我以下错误:
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
我也试过
RMatrix<char>
甚至手动cast in body 也没有成功.我哪里做错了??
and even manual cast in body but also without success. Where am I doing wrong??
任何建议和帮助将不胜感激.
Any suggestions and help would be highly appreciated.
YYA
推荐答案
这里的问题是 CharacterVector
和 CharacterMatrix
类型实际上只是 SEXP
s -- 更具体地说,一个 CharacterVector
包装了一个 STRSXP
,其内容是一个 SEXP
数组(运行时类型为 CHARSXP
).不幸的是,在与 R 的字符类型交互时,了解这些细节是必要的.
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.
我不知道这是否可行,但如果可行,您会想要使用类似 RMatrix<SEXP>
之类的东西,它(推测)会映射底层 CHARSXP
位于矩阵内部——您可以在其中使用 CHAR
访问带下划线的 const char*
字符串.
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
.
不幸的是,RMatrix
类不够聪明",无法在这种情况下直接提供对底层字符串的访问,所以我相信您必须自己解开它.
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.
这篇关于RcppParallel:将 CharacterMatrix 转换为 RMatrix<T>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!