是否可以直接将整数SEXP参数转换为整数而无需先将其转换为整数向量?
例:
#include <Rcpp.h>
SEXP f(SEXP n)
{
Rcpp::IntegerVector n_vec(n);
int n1 = n_vec[0];
...
return R_NilValue;
}
Run Code Online (Sandbox Code Playgroud)
当然 - as<>()转换器就是这样做的.
它可以被显式调用(这里你需要),有时被编译器隐式调用,或者甚至被代码生成助手插入,如下所示:
R> cppFunction('int twiceTheValue(int a) { return 2*a; }')
R> twiceTheValue(21)
[1] 42
R>
Run Code Online (Sandbox Code Playgroud)
如果使用参数调用cppFunction()(以及Rcpp属性或内联包中的相关函数)verbose=TRUE,则会看到生成的代码.
在这里,我明白了
#include <Rcpp.h>
RcppExport SEXP sourceCpp_47500_twiceTheValue(SEXP aSEXP) {
BEGIN_RCPP
Rcpp::RNGScope __rngScope;
int a = Rcpp::as<int >(aSEXP);
int __result = twiceTheValue(a);
return Rcpp::wrap(__result);
END_RCPP
}
Run Code Online (Sandbox Code Playgroud)
而我们的文档解释了BEGIN_RCPP,END_RCPP宏做,为什么RNGScope对象是存在的-你看到as<>()和wrap()你需要的.
| 归档时间: |
|
| 查看次数: |
2232 次 |
| 最近记录: |