Rcpp:错误:与请求的类型不兼容

时间:2014-10-03 14:39:42

标签: r rcpp

我有这个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
      }
}

和这个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)
}

在一个名为PoweR的软件包中(实际上上面的代码是对我自己的代码的简化,所以不要试图理解它的目的)。当我编译我的包(在Linux和R版本3.1.0下)并在控制台中发出以下R命令时:

require(PoweR)
compquant()

我收到以下错误: 错误:与请求的类型不兼容

您对可能出现的问题以及如何解决问题有所了解吗?

谢谢。

1 个答案:

答案 0 :(得分:2)

我只需删除第6行: Rcpp :: RNGScope __rngScope; 解决问题。话虽如此,Dirk Eddelbuettel对如何大大简化整个过程的rcpp-devel给出了非常好的提示。非常感谢Dirk。