當我們想將 Rcpp
中的多種類型的對象通過一個 return
函數返回時,此時就需要將我們的所有對象整理成一個 Rcpp::List
型,然后再進行返回。
但相比于 R 中的 list(mat1 = mat1, mat2 = mat2)
,Rcpp
中的列表創建就相對復雜一些,需要使用 create()
函數,如下面例子所示:
1
2
3
4
|
Rcpp::List ListFun(MatrixXd X ) { Eigen::MatrixXd mat1, mat2; return List: :create (Named( "matrix1" ) = mat1, Named( "matrix2" ) = mat2); } |
在 return
之后,我們想要在我們的 .cpp
文件中再調用這個 List
(或者直接讀取 R 中的 list
類型均可),這時我們應該怎么做呢?
其實也非常簡單,分兩步即可:第一步創建 List
,第二步分別創建 List
中的內容,對象類型對應上即可,如下所示:
1
2
3
4
5
6
7
8
9
10
|
void TestFun(MatrixXd X , MatrixXd Y ) { Rcpp::List result_x, result_y; result_x= ListFun( X ); result_y= ListFun( Y ); MatrixXd mat1_x = result_x[ "matrix1" ]; MatrixXd mat1_y = result_y[ "matrix1" ]; MatrixXd mat2_x = result_x[ "matrix2" ]; MatrixXd mat2_y = result_y[ "matrix2" ]; } |
以上就是R語言學習初識Rcpp類型List的詳細內容,更多關于R語言Rcpp初識List類型的資料請關注服務器之家其它相關文章!
原文鏈接:https://kanny.blog.csdn.net/article/details/102814882