Rcppのbig.matrixオブジェクトから列を抽出する関数を作成しようとしています(その結果をRに変換する前にcppで解析できるようにしています)しかし、NAの認識方法を理解することはできません(以下の最小限の例で示すように-2147483648として表示されます)。 Rcppから直接GetMatrixCols(src/bigmemory.cpp)の関数にアクセスできればさらに良いでしょうが、私はそれを行う方法をまだ発見していません。RcppのbigmemoryオブジェクトからNAを持つ列を抽出する
#include <Rcpp.h>
// [[Rcpp::plugins(cpp11)]]
// [[Rcpp::depends(BH, bigmemory)]]
#include <bigmemory/MatrixAccessor.hpp>
#include <bigmemory/isna.hpp>
using namespace Rcpp;
//Logic for extracting column from a Big Matrix object
template <typename T>
NumericVector GetColumn_logic(XPtr<BigMatrix> pMat, MatrixAccessor<T> mat, int cn) {
NumericVector nv(pMat->nrow());
for(int i = 0; i < pMat->nrow(); i++) {
if(isna(mat[cn][i])) {
nv[i] = NA_INTEGER;
} else {
nv[i] = mat[cn][i];
}
}
return nv;
}
//' Extract Column from a Big Matrix.
//'
//' @param pBigMat A bigmemory object address.
//' @param colNum Column Number to extract. Indexing starts from zero.
//' @export
// [[Rcpp::export]]
NumericVector GetColumn(SEXP pBigMat, int colNum) {
XPtr<BigMatrix> xpMat(pBigMat);
switch(xpMat->matrix_type()) {
case 1: return GetColumn_logic(xpMat, MatrixAccessor<char>(*xpMat), colNum);
case 2: return GetColumn_logic(xpMat, MatrixAccessor<short>(*xpMat), colNum);
case 4: return GetColumn_logic(xpMat, MatrixAccessor<int>(*xpMat), colNum);
case 6: return GetColumn_logic(xpMat, MatrixAccessor<float>(*xpMat), colNum);
case 8: return GetColumn_logic(xpMat, MatrixAccessor<double>(*xpMat), colNum);
default: throw Rcpp::exception("Unknown type detected for big.matrix object!");
}
}
/*** R
bm <- bigmemory::as.big.matrix(as.matrix(reshape2::melt(matrix(c(1:4,NA,6:20),4,5))))
bigmemory:::CGetType([email protected])
bigmemory:::GetCols.bm(bm, 3)
GetColumn([email protected], 2)
*/
ありがとうございました。本当にありがとうございました。私はGetMatrixCols関数を呼び出すことができれば簡単にできると思います。これは正確な操作を行うためだけでなく、他のプロジェクトでも同様のニーズがあるためです。 *自分のRcppコードでRcppエクスポート関数を呼び出す必要があります。**私は 'SEXP GetMatrixCols(SEXP bigMatAddr、SEXP col);を含むヘッダーファイルを作成し、bigmemoryの新しいバージョンを作成しましたが、sourceCpp:ed:' Error in dyn.load'。私は本当に馬鹿なことをしていますか? – samssan
Re 1):あなたは誤りだと思います。 NAのバイナリパターンは、 'double'から' int'への強制コピーで生き残れないかもしれません。入力時に 'double'を作成するだけです。 Re 2)と太字のテキスト。あなたが何を話しているのか分かりません。 * Rcpp属性を使用するすべての*パッケージはそれを行います。詳細については、Rcpp Attribributes vignetteを参照してください。 Re 3)あなたは、パッケージビルドでハングアップしていると思う。 rcpp-develへの新しい質問または投稿? –
これがもっと明確かどうかは分かりませんが、試してみます。 OtherPackageにはcpp関数があり、そのパッケージのヘッダファイルには定義されていません。私はcppレベルでMyPackageでその関数を使用する必要があります。私はここで同様の質問を見つけた[リンク](http://stackoverflow.com/questions/27079811/how-do-i-share-c-functions-in-rcpp-based-libraries-between-r-packages?rq= 1)。当分の間それに従おうとしています。ありがとうございました! – samssan