<html><body><div style="color:#000; background-color:#fff; font-family:arial, helvetica, sans-serif;font-size:10pt"><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; ">Hi,</div><div style="font-family: arial, helvetica, sans-serif; font-size: 10pt; "><br></div><div style="font-family: arial, helvetica, sans-serif; font-size: 13px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal; ">I'm currently using Rcpp and inline to speed up some R code I wrote, but being a C++/Rcpp newbie I'm running into some difficulties. More specifically, I'm having problems calling an Rcpp function inside another one. As a toy example consider the code:</div><div style="font-family: arial, helvetica, sans-serif; font-size: 13px; color: rgb(0, 0, 0); background-color: transparent; font-style: normal; ">## </div><div style="background-color: transparent; "><div style="background-color: transparent; "><font face="arial, helvetica,
sans-serif" size="2">library(Rcpp)</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">library(inline)</font></div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">code2.0 <-'</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> NumericVector u = as<NumericVector>(u_r);</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> double mu = as<double>(mu_r), </font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> v = log(mu);</font></div><div style="background-color: transparent; "><font face="arial, helvetica,
sans-serif" size="2"> int n = u.size(),</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> res = 0;</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><br></font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> for(int i=0; i<n; i++){</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> res += (exp(u(i))-v) > 0 ? 1 : 0;</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"> }</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><br></font></div><div style="background-color: transparent; "><font
face="arial, helvetica, sans-serif" size="2"> return wrap(res);</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">'</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">fun.test <- cxxfunction(sig2, code2.0, plugin = "Rcpp")</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><br></font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">fun.test(1:4, 100) </font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2">sum(exp(1:4) > log(100)) # ok!</font></div><div style="background-color: transparent; color:
rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; "><font size="2">##</font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2"><br></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2">It works just fine. Now, I'd like to break it down into two functions like so:</font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2">##</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><div style="background-color: transparent; ">code1 <-'</div><div style="background-color: transparent; "> double x =
as<double>(x_r),</div><div style="background-color: transparent; "> y = as<double>(y_r),</div><div style="background-color: transparent; "> ans;</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> ans = (exp(x)-y) > 0 ? 1 : 0;</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> return wrap(ans);</div><div style="background-color: transparent; ">'</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; ">code2.1 <-'</div><div style="background-color: transparent; "> NumericVector u = as<NumericVector>(u_r);</div><div style="background-color: transparent; "> double mu = as<double>(mu_r), </div><div style="background-color: transparent;
"> v = log(mu);</div><div style="background-color: transparent; "> int n = u.size(),</div><div style="background-color: transparent; "> res = 0;</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> for(int i=0; i<n; i++){</div><div style="background-color: transparent; "> res += as<int>(fun1(u(i),v));</div><div style="background-color: transparent; "> }</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> return wrap(res);</div><div style="background-color: transparent; ">'</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; ">sig1 <- signature(x_r = 'numeric', y_r = 'numeric')</div><div
style="background-color: transparent; ">sig2 <- signature(u_r = 'numeric', mu_r = 'numeric')</div><div style="background-color: transparent; ">fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), </div><div style="background-color: transparent; "> list(code1, code2.1), plugin = "Rcpp")</div></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2">##</font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2"><br></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-family: arial, helvetica, sans-serif; font-style: normal; font-size: 13px; "><font size="2">But I get the error message:</font></div><div
style="background-color: transparent; "><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"># file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"># file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to type 'SEXP'</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"># file5686a0c71e8.cpp:55:20: error: invalid cast from type 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'</font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><font face="arial, helvetica, sans-serif" size="2"><br></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial,
helvetica, sans-serif; font-style: normal; "><font face="arial, helvetica, sans-serif" size="2">I then tried to change the types of 'u(i)' and 'v' to SEXP, but I get the same error:</font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><font face="arial, helvetica, sans-serif" size="2">##</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><div style="background-color: transparent; ">code2.2 <-'</div><div style="background-color: transparent; "> NumericVector u = as<NumericVector>(u_r);</div><div style="background-color: transparent; "> double mu = as<double>(mu_r), </div><div style="background-color: transparent; "> v = log(mu);</div><div style="background-color:
transparent; "> int n = u.size(),</div><div style="background-color: transparent; "> res = 0;</div><div style="background-color: transparent; "> SEXP us, vs = (SEXP) v;</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> for(int i=0; i<n; i++){</div><div style="background-color: transparent; "> us = (SEXP) u(i);</div><div style="background-color: transparent; "> res += as<int>(fun1(us,vs));</div><div style="background-color: transparent; "> }</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; "> return wrap(res);</div><div style="background-color: transparent; ">'</div><div style="background-color: transparent; "><br></div><div style="background-color:
transparent; ">fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), </div><div style="background-color: transparent; "> list(code1, code2.2), plugin = "Rcpp")</div></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><font face="arial, helvetica, sans-serif" size="2">##</font></div><div style="background-color: transparent; "><font face="arial, helvetica, sans-serif" size="2"><div style="background-color: transparent; "># file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)':</div><div style="background-color: transparent; "># file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to type 'SEXP'</div><div style="background-color: transparent; "># file5686a0c71e8.cpp:55:20: error: invalid cast from type
'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP'</div><div style="background-color: transparent; "><br></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; ">Is there any way to make it work?</div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><br></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; ">Thanks,</div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; ">Rubem</div></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><font face="arial,
helvetica, sans-serif" size="2"><br></font></div><div style="background-color: transparent; color: rgb(0, 0, 0); font-size: 13px; font-family: arial, helvetica, sans-serif; font-style: normal; "><font face="arial, helvetica, sans-serif" size="2"><br></font></div></div></div></div></body></html>