Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rcpp: Error: not compatible with requested type

Tags:

r

rcpp

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.

like image 621
Lafaye de Micheaux Pierre Avatar asked Oct 03 '14 14:10

Lafaye de Micheaux Pierre


1 Answers

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.

like image 115
Lafaye de Micheaux Pierre Avatar answered Nov 11 '22 14:11

Lafaye de Micheaux Pierre