Rcpp: Error: not compatible with requested type

爷,独闯天下 提交于 2019-12-23 10:42:03

问题


I have this C++ code:

#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
      }
}

and this R code:

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)
}

in a package called PoweR (in fact the above codes are simplifications of my own code so do no try to understand the purpose of it). When I compile my package (under Linux and R version 3.1.0) and issue the following R command in the console:

require(PoweR)
compquant()

I get the following error: Error: not compatible with requested type

Do you have any idea on what could be the problem and how to solve it?

Thank you.


回答1:


I just had to remove the 6th line: Rcpp::RNGScope __rngScope; to solve the problem. This being said, Dirk Eddelbuettel gave very good hints on rcpp-devel on how to greatly simplify the whole process. So thank you very much Dirk.



来源:https://stackoverflow.com/questions/26181068/rcpp-error-not-compatible-with-requested-type

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!