Rcpp_eval 在传递未计算的参数时导致段错误

Rom*_*kyi 1 r rcpp

我想评估 Rcpp 函数内的变量

SEXP foo(SEXP arg) {
  SEXP x = NULL;
  try {
    x = Rcpp_eval(arg, Environment::global_env());
  } catch(...) {
    printf("Error\n");   
  }
  return x;
}
Run Code Online (Sandbox Code Playgroud)

如果arg初始化的话.GlobalEnv似乎没问题。

x <- 1
foo(substitute(x))
Run Code Online (Sandbox Code Playgroud)

但是如果arg没有初始化.GlobalEnv就会出现segfault

foo(substitute(y))
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?还是有问题Rcpp

Mar*_*gan 5

设置xR_NilValue在出错时返回 R 的 NULL,而不是 NULL 指针。想必您不想使用printf()来处理错误。我猜你的意思是

x = Rcpp_eval(arg, Environment::global_env());
Run Code Online (Sandbox Code Playgroud)

arg而不是mode)。

SEXP foo(SEXP arg) {
  SEXP x = R_NilValue;
  try {
    x = Rcpp_eval(arg, Environment::global_env());
  } catch(...) {
    printf("Error\n");
  }
  return x;
}
Run Code Online (Sandbox Code Playgroud)