簡體   English   中英

在R中使用C ++代碼

[英]using C++ code in R

我正在試驗Rcpp和內聯軟件包以加快計算速度。我想知道如何使C ++ CODE(不是c ++函數)與這些軟件包一起工作?

這是我嘗試的一個示例,其中包括構建一個名為“ tableau”的動態數組並顯示結果。 我都嘗試過'cppFunction'和'cxxfunction',但是沒有成功...有人可以給我提示嗎?

require(inline);require(Rcpp)

src='vector < vector < int > > tableau (

{

{1,2,3,42},

{0,2,3},

{11,12}

}

);

return tableau;
'

cppFunction(src)


Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  : 
  Error 1 occurred building shared library.
In addition: Warning message:
No function found for Rcpp::export attribute at file7bc1b0f5993.cpp:5 

R不知道如何處理<vector <vector <int>> 要返回列表,必須使用ListNumericVector類型:

src = 'List tableau() {
  NumericVector v1 = NumericVector::create(1,2,3,42);
  NumericVector v2 = NumericVector::create(0,2,3);
  NumericVector v3 = NumericVector::create(11,12); 

  return List::create(v1, v2, v3);
}'
createTableau <- cppFunction(src)
createTableau()
## [[1]]
## [1]  1  2  3 42
## 
## [[2]]
## [1] 0 2 3
##
## [[3]]
## [1] 11 12

您應該至少閱讀一些文檔。 這是一個不錯的起點: Rcpp教程

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM