<html><body><div style="color:#000; background-color:#fff; font-family:arial, helvetica, sans-serif;font-size:10pt"><div><span>I see. It's just that, for the time being, inline is pretty handy for a novice like me translating R code to C++. </span></div><div><br></div>  <div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "> <div style="font-family: 'times new roman', 'new york', times, serif; font-size: 12pt; "> <div dir="ltr"> <font size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">De:</span></b> Douglas Bates <bates@stat.wisc.edu><br> <b><span style="font-weight: bold;">Para:</span></b> Jeffrey Pollock <jeffpollock9@gmail.com> <br><b><span style="font-weight: bold;">Cc:</span></b> Rubem Kaipper Ceratti <rubem_ceratti@yahoo.com.br>; "rcpp-devel@lists.r-forge.r-project.org" <rcpp-devel@r-forge.wu-wien.ac.at> <br> <b><span style="font-weight: bold;">Enviadas:</span></b> Quarta-feira,
 10 de Outubro de 2012 12:10<br> <b><span style="font-weight: bold;">Assunto:</span></b> Re: [Rcpp-devel] 'Nested' Rcpp functions using inline<br> </font> </div> <br>Remember that the inline package is convenient for interactive<br>exploration during code development but usually the ultimate goal when<br>working with Rcpp is to produce a package including both R and C++<br>code.  When you start doing complicated things in the C++ code it is<br>probably time to look at creating a package.<br><br>On Wed, Oct 10, 2012 at 9:59 AM, Jeffrey Pollock <<a ymailto="mailto:jeffpollock9@gmail.com" href="mailto:jeffpollock9@gmail.com">jeffpollock9@gmail.com</a>> wrote:<br>> I think you need to use the `includes` argument of cxxfunction to include a<br>> pure c++ function ie;<br>><br>> library(inline)<br>> library(Rcpp)<br>><br>> inc <-'<br>>         int fun1(double x, double y) {<br>>     
        return (exp(x) - y) > 0 ? 1 : 0;<br>>         }<br>>         '<br>><br>> body <-'<br>><br>>         NumericVector u   = as<NumericVector>(u_r);<br>>         double        mu  = as<double>(mu_r),<br>>         v   = log(mu);<br>>         int           n   = u.size(),<br>>         res = 0;<br>><br>>         for(int i=0; i<n; i++){<br>>             res += fun1(u(i), v);<br>>         }<br>><br>>         return wrap(res);<br>>         '<br>><br>> fun.test <- cxxfunction(signature(u_r = 'numeric', mu_r = 'numeric'), body,<br>> "Rcpp",
 inc)<br>><br>>> identical(fun.test(1:4, 100), sum(exp(1:4) > log(100)))<br>> [1] TRUE<br>><br>> On Wed, Oct 10, 2012 at 3:39 PM, Rubem Kaipper Ceratti<br>> <<a ymailto="mailto:rubem_ceratti@yahoo.com.br" href="mailto:rubem_ceratti@yahoo.com.br">rubem_ceratti@yahoo.com.br</a>> wrote:<br>>><br>>> Hi,<br>>><br>>> I'm currently using Rcpp and inline to speed up some R code I wrote, but<br>>> being a C++/Rcpp newbie I'm running into some difficulties. More<br>>> specifically, I'm having problems calling an Rcpp function inside another<br>>> one. As a toy example consider the code:<br>>> ##<br>>> library(Rcpp)<br>>> library(inline)<br>>><br>>> code2.0 <-'<br>>>   NumericVector u   = as<NumericVector>(u_r);<br>>>   double        mu  = as<double>(mu_r),<br>>>       
          v   = log(mu);<br>>>   int           n   = u.size(),<br>>>                 res = 0;<br>>><br>>>   for(int i=0; i<n; i++){<br>>>     res += (exp(u(i))-v) > 0 ? 1 : 0;<br>>>   }<br>>><br>>>   return wrap(res);<br>>> '<br>>> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')<br>>> fun.test <- cxxfunction(sig2, code2.0, plugin = "Rcpp")<br>>><br>>> fun.test(1:4, 100)<br>>> sum(exp(1:4) > log(100))  # ok!<br>>> ##<br>>><br>>> It works just fine. Now, I'd like to break it down into two functions like<br>>> so:<br>>> ##<br>>> code1 <-'<br>>>   double x = as<double>(x_r),<br>>>          y = as<double>(y_r),<br>>>   
       ans;<br>>><br>>>   ans = (exp(x)-y) > 0 ? 1 : 0;<br>>><br>>>   return wrap(ans);<br>>> '<br>>><br>>> code2.1 <-'<br>>>   NumericVector u   = as<NumericVector>(u_r);<br>>>   double        mu  = as<double>(mu_r),<br>>>                 v   = log(mu);<br>>>   int           n   = u.size(),<br>>>                 res = 0;<br>>><br>>>   for(int i=0; i<n; i++){<br>>>     res += as<int>(fun1(u(i),v));<br>>>   }<br>>><br>>>   return wrap(res);<br>>> '<br>>><br>>> sig1 <- signature(x_r = 'numeric', y_r = 'numeric')<br>>> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')<br>>>
 fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2),<br>>>                         list(code1, code2.1), plugin = "Rcpp")<br>>> ##<br>>><br>>> But I get the error message:<br>>> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':<br>>> #   file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to<br>>> type 'SEXP'<br>>> # file5686a0c71e8.cpp:55:20: error: invalid cast from type<br>>> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'<br>>><br>>> I then tried to change the types of 'u(i)' and 'v' to SEXP, but I get the<br>>> same error:<br>>> ##<br>>> code2.2 <-'<br>>>   NumericVector u   = as<NumericVector>(u_r);<br>>>   double        mu  = as<double>(mu_r),<br>>>     
            v   = log(mu);<br>>>   int           n   = u.size(),<br>>>                 res = 0;<br>>>   SEXP          us, vs = (SEXP) v;<br>>><br>>>   for(int i=0; i<n; i++){<br>>>     us = (SEXP) u(i);<br>>>     res += as<int>(fun1(us,vs));<br>>>   }<br>>><br>>>   return wrap(res);<br>>> '<br>>><br>>> fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2),<br>>>                         list(code1, code2.2), plugin = "Rcpp")<br>>> ##<br>>> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':<br>>> #   file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to<br>>> type
 'SEXP'<br>>> # file5686a0c71e8.cpp:55:20: error: invalid cast from type<br>>> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'<br>>><br>>> Is there any way to make it work?<br>>><br>>> Thanks,<br>>> Rubem<br>>><br>>><br>>><br>>> _______________________________________________<br>>> Rcpp-devel mailing list<br>>> <a ymailto="mailto:Rcpp-devel@lists.r-forge.r-project.org" href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>>> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br>><br>><br>><br>> _______________________________________________<br>> Rcpp-devel mailing list<br>> <a ymailto="mailto:Rcpp-devel@lists.r-forge.r-project.org"
 href="mailto:Rcpp-devel@lists.r-forge.r-project.org">Rcpp-devel@lists.r-forge.r-project.org</a><br>> <a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel</a><br><br><br> </div> </div>  </div></body></html>