2015-11-26 60 views
1

我是初學者到Rcpp我有這個錯誤信息來運行下面的R代碼。我使用的是Windows 10 「錯誤compileCode(F,代碼,語言=語言,詳細=詳細): 編譯錯誤,功能(S)/方法(S)中創建的警告信息:」Rcpp初學者錯誤消息

incltxt <- ' 
int fibonacci(const int x) { 
if (x == 0) return(0); 
if (x == 1) return(1); 
return fibonacci(x - 1) + fibonacci(x - 2); 
}' 


fibRcpp <- cxxfunction(signature(xs="int"), 
plugin="Rcpp", 
incl=incltxt, 
body=' 
int x = Rcpp::as<int>(xs); 
return Rcpp::wrap(fibonacci(x)); 
') 

回答

2

考慮簡單的和新cppFunction()

R> library(Rcpp) 
R> cppFunction('int f(int n) { if (n < 2) return n; return f(n-1) + f(n-2);}') 
R> f(10) 
[1] 55 
R> 

編輯:這裏是你的代碼修復。您還需要加載RCPP有它的插件註冊:

R> library(Rcpp)                       
R> library(inline)                      
R> incltxt <- '                       
+ int fibonacci(const int x) {                   
+ if (x == 0) return(0);                     
+ if (x == 1) return(1);                     
+ return fibonacci(x - 1) + fibonacci(x - 2);               
+ }'                          
R> bodytxt <- '                       
+ int x = Rcpp::as<int>(xs);                    
+ return Rcpp::wrap(fibonacci(x));                  
+ '                          
R> fibRcpp <- inline::cxxfunction(signature(xs="int"), incl=incltxt, body=bodytxt, plugin="Rcpp")  
R> fibRcpp(10) 
R> 55 
+0

它cppFunction的偉大工程,但如果我想使用incltxt我怎麼能與cppFunction代碼? – Shin

+0

爲什麼你堅持cppFunction?它來自內聯包;我們現在擁有的更好。 –

+0

我的主要問題是使我的Gibbs採樣代碼更快,因爲它在我的項目中使用了太多次,大約需要1天才能完成運行。我認爲解決方案可能會將我的R Gibbs採樣代碼編寫爲C++中的文本,如incltxt。 – Shin