Dear Dirk,

Thank you very much for this. I modified slightly your own code so that it is very similar (for the names of the arguments) to the one on StackOverflow:
http://stackoverflow.com/questions/26181068/rcpp-error-not-compatible-with-requested-type

This gives me this code saved in /tmp/pierre.cpp:

-----------------------------------------------------------------------------------------------
#include <Rcpp.h>

// [[Rcpp::export]]
Rcpp::List gensampleRcpp2(Rcpp::Function rlawfunc, Rcpp::IntegerVector n) {
  Rcpp::RNGScope __rngScope;
  return Rcpp::List::create(Rcpp::Named("sample") = rlawfunc(n),
                            Rcpp::Named("law.name") = " ",
                            Rcpp::Named("law.pars") = R_NilValue);
}

// [[Rcpp::export]]
Rcpp::List compQuantRcpp2(Rcpp::IntegerVector n, Rcpp::IntegerVector M, Rcpp::Function Rlaw) {
  int i;
  GetRNGstate();
  for (i=1;i<=M[0];i++) {
    Rcpp::List resultsample = gensampleRcpp2(Rlaw, n);
    Rcpp::NumericVector mysample = resultsample["sample"];
  }
   PutRNGstate();
   return Rcpp::List::create(Rcpp::Named("law.pars") = "");
}


/*** R
res <- compQuantRcpp2(n=50, M=10^3, Rlaw=rnorm)
print(str(res))
*/
-----------------------------------------------------------------------------------------------

Please have a look to compare both codes. They are very similar.

I then issue this command in R:
for (i in 1:100)  Rcpp::sourceCpp("tmp/pierre.cpp")

Magic! No error occur.

So now I will try to implement all this in a package (I guess I should read more carefully the Attributes vignette that you told me about. Am I right? Is it the good document to read for that purpose?).

By the way, since both codes are such similar, would you have an explanation on what goes wrong with what I have written on StackOverflow?

Regards,

Pierre L.



Le 03/10/2014 12:49, Dirk Eddelbuettel a écrit :
Oh, and I missed one call to a subordinate function.  So add

// [[Rcpp::export]]
SEXP newCompQuant2(int n, int M, Rcpp::Function f) {
   SEXP res = compquantRcpp2(n, M, f);
   return res;
}

and at the bottom call it:

res <- newCompQuant2(n=50, M=10^3, f=rnorm)
print(str(res))

and then the result is

R> res <- newCompQuant2(n=50, M=10^3, f=rnorm)

R> print(str(res))
List of 1
  $ law.pars: chr ""
NULL
R>

just like you were told as well on StackOverflow.

Dirk


_______________________________________________
Rcpp-devel mailing list
Rcpp-devel@lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to