我有这个C ++代码:
#include <R.h>
#include <Rcpp.h>
using namespace Rcpp;
extern "C" {
SEXP gensampleRcpp2( Function rlawfunc, SEXP n) {
Rcpp::RNGScope __rngScope;
return Rcpp::List::create(Rcpp::Named("sample") = rlawfunc(n),
Rcpp::Named("law.name") = " ",
Rcpp::Named("law.pars") = R_NilValue);
}
RcppExport SEXP gensampleRcpp(SEXP rlawfuncSEXP, SEXP nSEXP) {
BEGIN_RCPP
Function rlawfunc = Rcpp::as<Function >(rlawfuncSEXP);
IntegerVector n = Rcpp::as<IntegerVector >(nSEXP);
SEXP __result = gensampleRcpp2(rlawfunc, n);
return Rcpp::wrap(__result);
END_RCPP
}
SEXP compquantRcpp2(IntegerVector n, IntegerVector M, Function Rlaw) {
int i;
GetRNGstate();
for (i=1;i<=M[0];i++) {
List resultsample = gensampleRcpp2(Rlaw, n);
NumericVector mysample = Rcpp::as<NumericVector >(resultsample["sample"]);
}
PutRNGstate();
return Rcpp::List::create(Rcpp::Named("law.pars") = "");
}
RcppExport SEXP compquantRcpp(SEXP nSEXP, SEXP MSEXP, SEXP RlawSEXP) {
BEGIN_RCPP
IntegerVector n = Rcpp::as<IntegerVector >(nSEXP);
IntegerVector M = Rcpp::as<IntegerVector >(MSEXP);
Function Rlaw = Rcpp::as<Function >(RlawSEXP);
SEXP __result = compquantRcpp2(n, M, Rlaw);
return Rcpp::wrap(__result);
END_RCPP
}
}
Run Code Online (Sandbox Code Playgroud)
和这个R代码:
compquant <- function(n=50,M=10^3,Rlaw=rnorm) {
out <- .Call("compquantRcpp",n=as.integer(n),M=as.integer(M),as.function(Rlaw),PACKAGE="PoweR")
return(out)
}
Run Code Online (Sandbox Code Playgroud)
在名为PoweR的程序包中(实际上,上面的代码是我自己的代码的简化,因此请不要尝试理解它的目的)。当我编译软件包(在Linux和R版本3.1.0下)并在控制台中发出以下R命令时:
require(PoweR)
compquant()
Run Code Online (Sandbox Code Playgroud)
我收到以下错误: 错误:与请求的类型不兼容
您对可能的问题以及如何解决有任何想法吗?
谢谢。
我只需删除第 6 行: Rcpp::RNGScope __rngScope; 来解决问题。话虽这么说,Dirk Eddelbuettel 在 rcpp-devel 上给出了关于如何大大简化整个过程的很好的提示。非常感谢德克。
| 归档时间: |
|
| 查看次数: |
5853 次 |
| 最近记录: |