2016-11-17 78 views
0

我正在使用RcppGSL圍繞C庫創建包裝程序包。我遇到的,當我試圖返回它來自一個gsl_matrix *獲取錯誤無法將const RcppGSL :: matrix <double>轉換爲初始化中的「SEXP」

cannot convert const RcppGSL::matrix<double> to "SEXP" in initialization 

這裏結果的錯誤是造成問題的是

try { 
    exp.run_experiment(graphm_obj_A, graphm_obj_B); 
     gsl_matrix *tmp = exp.get_P_result(0) ; 
    RcppGSL::Matrix P(tmp); // ((gsl_matrix *) exp.get_P_result(0)) ; 
//const RcppGSL::matrix<double> P (gsl_matrix_ 

     Rcpp::List res = Rcpp::List::create(
    Rcpp::Named("debugprint_file") = algorithm_params["debugprint_file"], 
     //); 
      // 
    Rcpp::Named("Pmat") = P); 
    P.free(); 
    return res; 
    } catch(std::exception &ex) { 
     Rf_error(ex.what()); 
    } catch (...) { 
    return Rcpp::List(); 
    } 

如果我不嘗試返回P中的代碼,該軟件包的構建沒有錯誤。因此wrap.h中的相關行(下面添加)顯示這是由於不能隱式轉換爲SEXP。但我的理解是,RcppGSL :: Matrix對象被隱式轉換爲SEXP對象。我缺少

/** 
* This is the worst case : 
* - not a primitive 
* - not implicitely convertible tp SEXP 
* - not iterable 
* 
* so we just give up and attempt to use static_assert to generate 
* a compile time message if it is available, otherwise we use 
* implicit conversion to SEXP to bomb the compiler, which will give 
* quite a cryptic message 
*/ 
template <typename T> 
inline SEXP wrap_dispatch_unknown_iterable(const T& object, ::Rcpp::traits::false_type){ 
RCPP_DEBUG_1("wrap_dispatch_unknown_iterable<%s>(., false )", DEMANGLE(T)) 
     // here we know that T is not convertible to SEXP 
#ifdef HAS_STATIC_ASSERT 
    static_assert(!sizeof(T), "cannot convert type to SEXP") ; 
#else 
    // leave the cryptic message 
    SEXP x = object ; 
+2

請仔細研究RcppGSL中的_working_例子,看看它們如何處理GSL類型。還學會使用Rcpp屬性;你有太多的樣板代碼,高於此代碼是爲我們生成的,並且通常是隱藏的,讓我們專注於重要的事情。 –

回答

1

這裏得到一個矩陣GSL,有它通過將其添加到自身,並返回其轉化的一個很小的例子。

這是所有包含在這一個文件:

#include <RcppGSL.h> 

// [[Rcpp::depends(RcppGSL)]] 

// [[Rcpp::export]] 
RcppGSL::Matrix doubleMe(RcppGSL::Matrix X) { 
    RcppGSL::Matrix res = X; 
    gsl_matrix_add(res, X); // adds X to res 

    return res; 
} 

/*** R 
M <- matrix(1:9, 3, 3) 
doubleMe(M) 
*/ 

其中,採購的時候,不僅是編譯,鏈接和加載,但也執行在底部的標記爲R的註釋的「單元測試」:

R> sourceCpp("/tmp/GSLmatrixEx.cpp") 

R> M <- matrix(1:9, 3, 3) 

R> doubleMe(M) 
    [,1] [,2] [,3] 
[1,] 2 8 14 
[2,] 4 10 16 
[3,] 6 12 18 
R> 

從GSL中獲取矩陣並沒有真正的複雜性。由於RcppGSL::Matrix僅僅是一個更具體的RcppGSL::matrix<double>的typedef;它們當然是可以互換的。

相關問題