[Rcpp-devel] Rcpp: Error: not compatible with requested type

Pierre Lafaye de Micheaux lafaye at dms.umontreal.ca
Sat Oct 4 20:36:17 CEST 2014


Thank you very much. That helped a lot.

Have a nice day.

Pierre L.

Le 03/10/2014 18:57, Dirk Eddelbuettel a écrit :
> Hi Pierre,
>
> On 3 October 2014 at 18:19, Pierre Lafaye de Micheaux wrote:
> | 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;
>       ^^^^^^^^^^^^^^^^^^^^^^^^^^
>
> Not needed. Rcpp Attributes adds that for your, and better.
>
> |    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();
>
> Not needed. Rcpp Attributes deals with that via RNGScope.
>
> |    for (i=1;i<=M[0];i++) {
> |      Rcpp::List resultsample = gensampleRcpp2(Rlaw, n);
> |      Rcpp::NumericVector mysample = resultsample["sample"];
> |    }
> |     PutRNGstate();
>
> Ditto.
>
> So try without, and for kicks add   verbose=TRUE  to the call to sourceCpp().
>
> |     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?
>
> I don't have time to drill through it function by function. The gist of it is
> that you were doing A LOT of forced manual conversions, and one of them goes
> bad.  If you really want to know, add prints statements (or step through) and
> simplify.
>
> What you sent, even after your numerous prior emails was still not a
> __minimally__ reproducible example.
>
> The more complex the code is that you submit, the less likely it is
> volunteers will spend _their_ going through it for you.
>
> Dirk
>   
> | 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 at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>



More information about the Rcpp-devel mailing list