我想评估 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?
设置x为R_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)