RStudio 与 Rcpp 不断崩溃

RStudio keeps crashing with Rcpp

我想了解导致此程序崩溃的原因。我已经看到至少三个相关问题 here, here and here,但我还没有找到解决该问题的明确答案,因此这里有一些示例代码可以重现该问题。

R代码:

library(Rcpp)

Rcpp::sourceCpp("myfunction.cpp")

data1      <- rnorm(2000)
data2      <- rnorm(2000)

mydata <- matrix(cbind(data1, data2), nrow=2000, ncol=2)
values <- log(1:6)

for (i in 1:1000) {
  myfunction(values, mydata)
}

C++代码:

#include "Rcpp.h"
#include "math.h"
using namespace Rcpp;

// [[Rcpp::export]]
double myfunction(const NumericVector& theta, const NumericMatrix& data) {

  double ans = 0;

  int theta_size = theta.length();
  NumericVector mytheta(theta_size);

  int data_size = data.nrow();
  NumericMatrix mat(data_size, 2);

  for (int i = 0; i < theta_size; i++) {
    mytheta(i) = exp(theta(i));
  }

  if ( true ) {  // Flow control

    for (int i = 0; i < data_size; i++) {
      mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(1)*mat(i-1, 1);
      ans = ans + 1;
    }

    for (int i = 0; i < data_size; i++) {
      mat(i, 2) = pow(data(i-1, 2), 2) + mytheta(4)*mat(i-1, 2);
      ans = ans + 1;
    }

  }

  Rcout << "Ok!\n";

  return ans;
}

一切正常至少第一次我使用myfunction(),但在 R for 循环中调用时它会崩溃。我重新安装了 R、Rtools 和 RStudio(在 Windows 上)以查看安装是否有问题,但我仍然面临同样的问题。

这使得 R 和 C++ 之间的无缝集成变得不像我最初想的那样无缝,而且由于我发现我不是唯一一个面临这个问题的人,所以看起来我们都在做一些使用 Rcpp(至少在 RStudio 上)开始时出现明显错误,但它是什么?

基本上,我想确定我没有遗漏一些非常明显的东西,因为到目前为止我看到的所有答案似乎都暗示情况确实如此。

注意:这是我一直在用 Rcpp 测试的较长函数的缩短版本,原始版本似乎在第一个 少数 次我调用它,但它最终也崩溃了。

更新:删除了 rm(),糟糕。

for (int i = 0; i < data_size; i++) {
  mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(1)*mat(i-1, 1);
  ans = ans + 1;
}

i == 0 时,您尝试访问 data(-1, 1),但事情从那里开始变得糟糕。事实上,它没有在你第一次崩溃时 运行 这只是意味着你很幸运(或不幸,因为它引诱你进入一种错误的自信感)。

所以这里是修复后的版本

  • @HongOoi 对循环指数的修正
  • 我对专栏的更正
  • C++文件中的R代码
  • 调用 N 次的 C++ 代码没有输出
  • 最后输出
  • 为 RNG 播种以使其可重现

代码

#include "Rcpp.h"
#include "math.h"
using namespace Rcpp;

// [[Rcpp::export]]
double myfunction(const NumericVector& theta, const NumericMatrix& data) {

  double ans = 0;

  int theta_size = theta.length();
  NumericVector mytheta(theta_size);

  int data_size = data.nrow();
  NumericMatrix mat(data_size, 2);

  for (int i = 0; i < theta_size; i++) {
    mytheta(i) = exp(theta(i));
  }

  if ( true ) {  // Flow control

    for (int i = 1; i < data_size; i++) {
      mat(i, 0) = pow(data(i-1, 0), 2) + mytheta(1)*mat(i-1, 0);
      ans = ans + 1;
    }

    for (int i = 1; i < data_size; i++) {
      mat(i, 1) = pow(data(i-1, 1), 2) + mytheta(4)*mat(i-1, 1);
      ans = ans + 1;
    }

  }

  //Rcout << "Ok!\n";

  return ans;
}

/**** R
set.seed(123)
data1      <- rnorm(2000)
data2      <- rnorm(2000)

mydata <- matrix(cbind(data1, data2), nrow=2000, ncol=2)
values <- log(1:6)

for (i in 1:1000) {
  myfunction(values, mydata)
}
cat("Success\n")
*/

演示

edd@rob:~$ Rscript -e 'Rcpp::sourceCpp("/tmp/trusky.cpp")'

R> set.seed(123)

R> data1 <- rnorm(2000)

R> data2 <- rnorm(2000)

R> mydata <- matrix(cbind(data1, data2), nrow = 2000, 
+     ncol = 2)

R> values <- log(1:6)

R> for (i in 1:1000) {
+     myfunction(values, mydata)
+ }

R> cat("Success\n")
Success
edd@rob:~$