【发布时间】:2018-12-27 18:15:00
【问题描述】:
我有一个简单的rcpp 文件,它会导致 RStudio 崩溃(或者看起来如此)。
C++ 代码
这是.cpp文件(另存为Test/CppHelpers.cpp):
#include <Rcpp.h>
// [[Rcpp::plugins("cpp11")]]
using namespace Rcpp;
// [[Rcpp::export]]
NumericMatrix generateCombinations(const NumericMatrix& odds) {
int n = odds.rows();
int N = n*n*n;
IntegerVector v1(n);
std::iota(v1.begin(), v1.end(), 0);
IntegerVector v2(n*n);
v2 = rep_each(v1,n);
NumericVector col0(N);
NumericVector col1(N);
NumericVector col2(N);
for(int k = 0; k <= N; ++k) {
int ind0 = k / (n*n);
int ind1 = k % (n*n);
ind1 = v2[ind1];
int ind2 = k % n;
col0[k] = odds(ind0,0);
col1[k] = odds(ind1,1);
col2[k] = odds(ind2,2);
}
NumericMatrix out(N,3);
out(_,0) = col0;
out(_,1) = col1;
out(_,2) = col2;
return out;
}
R 代码
这是我的.R 文件,我只是在其中获取上述文件并执行基本测试
Rcpp::sourceCpp("Test/CppHelpers.cpp")
n <- 9
odds <- matrix(1:n,ncol=3)
my_combs <- generateCombinations(odds)
这实际上按预期工作。但是,每次我运行代码时,一段时间后,它总是会导致 RStudio 崩溃。
我真的不明白为什么会出现这些崩溃以及如何解决它(尤其是因为它不会立即崩溃)。任何指导将不胜感激。
会话信息
> sessionInfo()
R version 3.5.1 (2018-07-02)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
Matrix products: default
locale:
[1] LC_COLLATE=German_Germany.1252 LC_CTYPE=German_Germany.1252 LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C LC_TIME=German_Germany.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_3.5.1 tools_3.5.1 yaml_2.2.0
【问题讨论】:
-
您是否在没有 RStudio 的情况下在 R 中尝试过这个?我的猜测是,这与后者无关(因此与rstudio 标签无关)。
-
@r2evans 不,我没有,我会在
RGui中尝试它,我会删除 - 至少现在 -rstudio标签。 -
基本错误:
k <= N。你希望它是k < N。 C(++) 索引从 0 到 N-1。 -
@DirkEddelbuettel 它一直在盯着我看——感觉很愚蠢。感谢您的帮助!
-
@AGrothedieck 相信我,我们都去过那里。不用担心:)