2017-05-28 61 views
0

我在R包中使用RcppArmadillo,我想在參數列表中使用Rcpp :: Nullable。使用Rcpp :: Nullable與RcppArmadillo類型錯誤

NumericVector d_snb(NumericVector& x, 
       Nullable<arma::mat> size_param = R_NilValue, 
       const int& infinite = 100000, const bool& log_v = false) 

這給瞭如下錯誤:

Error: package or namespace load failed for ‘packagex’ in dyn.load(file, DLLpath = DLLpath, ...): unable to load shared object '/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so': dlopen(/usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so, 6): 
Symbol not found: __Z5d_snbRN4Rcpp6VectorILi14ENS_15PreserveStorageEEENS_8NullableIS2_EES5_S5_RKiRKb 
Referenced from: /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so 
Expected in: flat namespace in /usr/local/lib/R/3.4/site-library/packagex/libs/packagex.so 
Error: loading failed 
Execution halted 

惟一可行的辦法,現在似乎對參數作爲NumericVector然後獲取內容,然後將它扔在犰狳的類型。

NumericVector d_snb(NumericVector& x, 
       Nullable<NumericVector> size_param = R_NilValue ...) 
{ 
... 

    if(size_param.isNotNull()) { 
    arma::mat test(NumericVector(size_param)); 
    param_check++; 
    } 
... 
} 

這給出了一個警告:

d_snb.cpp:36:21: warning: parentheses were disambiguated as a function declaration [-Wvexing-parse] 
    arma::mat test(NumericVector(size_param)); 
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~ 
d_snb.cpp:36:22: note: add a pair of parentheses to declare a variable 
    arma::mat test(NumericVector(size_param)); 
       ^
       (      ) 
1 warning generated. 

什麼是去了解它的最好方法?

回答

2

是的,Rcpp::Nullable<>只適用於基於SEXP的Rcpp類型。

所以你必須做你發現的兩步過程。但我認爲你可以做一個簡單:arma::mat test = Rcpp::as<arma::mat>(size_param);

這裏是一個編譯一個完整的例子(但「無」):

#include <RcppArmadillo.h> 
// [[Rcpp::depends(RcppArmadillo)]] 

// [[Rcpp::export]] 
void d_snb(Rcpp::NumericVector& x, 
      Rcpp::Nullable<Rcpp::NumericVector> size_param = R_NilValue) { 

    if (size_param.isNotNull()) { 
    arma::vec test = Rcpp::as<arma::vec>(size_param); 
    test.print("vector"); 
    } 
    Rcpp::Rcout << x << std::endl; 

} 

和快速演示:

R> sourceCpp("/tmp/so44102346.cpp") 
R> d_snb(c(1,2,3)) 
1 2 3 
R> d_snb(c(1,2,3), c(4,5,6)) 
vector 
    4.0000 
    5.0000 
    6.0000 
1 2 3 
R> 
+1

好的解決方案。但是可以通過引用傳遞Rcpp :: Nullable 。如果我使用它,可能的參數將是一個大的向量。所以我不想複製一份 –