2014-04-10 67 views
1

使用RcppArmadillo :: sample函數時,我發現使用大輸入向量會導致RStudio崩潰。我提供以下全部代碼:RcppArmadillo ::示例函數導致RStudio和R崩潰

#include<iostream> 
#include <armadillo> 
#include <RcppArmadilloExtensions/sample.h> 
//[[Rcpp::depends(RcppArmadillo)]] 

using namespace std; 
using namespace Rcpp; 
using namespace arma; 

//[[Rcpp::export]] 
IntegerVector test_func(int N) { 
    IntegerVector frame = Range(1, N); 
    NumericVector wts = runif(N, 0, 1); 
    NumericVector Wts = wts/sum(wts); 

    IntegerVector y = RcppArmadillo::sample(frame, N,TRUE, Wts); 
    return y; 
} 

調用test_func(N=100)會產生正確的結果。但N大於200,例如test_func(N=210),會導致RStudio以及RConsole崩潰。我正在犯錯誤嗎?

回答

3

我無法複製這個。無論是直播還是RStudio,它都適合我。

我做了小的修正代碼:

// the following header also include Rcpp and Armadillo headers 
#include <RcppArmadilloExtensions/sample.h> 

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

//[[Rcpp::export]] 
Rcpp::IntegerVector test_func(int N) { 
    Rcpp::IntegerVector frame = Rcpp::Range(1, N); 
    Rcpp::NumericVector wts = Rcpp::runif(N, 0.0, 1.0); 
    return Rcpp::RcppArmadillo::sample(frame, N, true, wts/Rcpp::sum(wts)); 
} 

但這些都不應該是材料。

注意sample()函數的代碼如何拋出一個錯誤時拋出,如果N得到大:

if (walker_test < 200) { 
    ProbSampleReplace<IntegerVector>(index, nOrig, size, prob); 
} else { 
    throw std::range_error("Walker Alias method not implemented. [...]"); 
} 

所以我覺得你可能會看到R,RCPP,RcppArmadillo之間的不匹配的花園各種錯誤。你在哪個平臺上?因爲它是重新編譯包的Linux。

+0

我在Mac OS X 10.9.2版本上使用R 3.0.3「Warm Puppy」。然而,現在我更新到R 3.1.0「Sun Dance」,並且我收到了錯誤信息「Walker Alias method not implemented」,這是我以前沒有在「Warm Puppy」中找到的。謝謝你的提示! – NightOwl85

+0

我非常確信你之前只是在Rcpp和RcppArmadillo之間有一個不匹配的地方---一個地方重建,一個沒有。這是行不通的,因此觸發的異常使您的會話失效。 sample()現在是「舊」的,並且在每個版本中都進行了單元測試。 –