Remember that the inline package is convenient for interactive exploration during code development but usually the ultimate goal when working with Rcpp is to produce a package including both R and C++ code. When you start doing complicated things in the C++ code it is probably time to look at creating a package.
On Wed, Oct 10, 2012 at 9:59 AM, Jeffrey Pollock <[email protected]> wrote: > I think you need to use the `includes` argument of cxxfunction to include a > pure c++ function ie; > > library(inline) > library(Rcpp) > > inc <-' > int fun1(double x, double y) { > return (exp(x) - y) > 0 ? 1 : 0; > } > ' > > body <-' > > NumericVector u = as<NumericVector>(u_r); > double mu = as<double>(mu_r), > v = log(mu); > int n = u.size(), > res = 0; > > for(int i=0; i<n; i++){ > res += fun1(u(i), v); > } > > return wrap(res); > ' > > fun.test <- cxxfunction(signature(u_r = 'numeric', mu_r = 'numeric'), body, > "Rcpp", inc) > >> identical(fun.test(1:4, 100), sum(exp(1:4) > log(100))) > [1] TRUE > > On Wed, Oct 10, 2012 at 3:39 PM, Rubem Kaipper Ceratti > <[email protected]> wrote: >> >> Hi, >> >> 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: >> ## >> library(Rcpp) >> library(inline) >> >> code2.0 <-' >> NumericVector u = as<NumericVector>(u_r); >> double mu = as<double>(mu_r), >> v = log(mu); >> int n = u.size(), >> res = 0; >> >> for(int i=0; i<n; i++){ >> res += (exp(u(i))-v) > 0 ? 1 : 0; >> } >> >> return wrap(res); >> ' >> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric') >> fun.test <- cxxfunction(sig2, code2.0, plugin = "Rcpp") >> >> fun.test(1:4, 100) >> sum(exp(1:4) > log(100)) # ok! >> ## >> >> It works just fine. Now, I'd like to break it down into two functions like >> so: >> ## >> code1 <-' >> double x = as<double>(x_r), >> y = as<double>(y_r), >> ans; >> >> ans = (exp(x)-y) > 0 ? 1 : 0; >> >> return wrap(ans); >> ' >> >> code2.1 <-' >> NumericVector u = as<NumericVector>(u_r); >> double mu = as<double>(mu_r), >> v = log(mu); >> int n = u.size(), >> res = 0; >> >> for(int i=0; i<n; i++){ >> res += as<int>(fun1(u(i),v)); >> } >> >> return wrap(res); >> ' >> >> sig1 <- signature(x_r = 'numeric', y_r = 'numeric') >> sig2 <- signature(u_r = 'numeric', mu_r = 'numeric') >> fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), >> list(code1, code2.1), plugin = "Rcpp") >> ## >> >> But I get the error message: >> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)': >> # file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to >> type 'SEXP' >> # file5686a0c71e8.cpp:55:20: error: invalid cast from type >> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP' >> >> I then tried to change the types of 'u(i)' and 'v' to SEXP, but I get the >> same error: >> ## >> code2.2 <-' >> NumericVector u = as<NumericVector>(u_r); >> double mu = as<double>(mu_r), >> v = log(mu); >> int n = u.size(), >> res = 0; >> SEXP us, vs = (SEXP) v; >> >> for(int i=0; i<n; i++){ >> us = (SEXP) u(i); >> res += as<int>(fun1(us,vs)); >> } >> >> return wrap(res); >> ' >> >> fun.test <- cxxfunction(list(fun1 = sig1, fun2 = sig2), >> list(code1, code2.2), plugin = "Rcpp") >> ## >> # file5686a0c71e8.cpp: In function 'SEXPREC* fun2(SEXP, SEXP)': >> # file5686a0c71e8.cpp:52:33: error: invalid cast from type 'double' to >> type 'SEXP' >> # file5686a0c71e8.cpp:55:20: error: invalid cast from type >> 'Rcpp::traits::storage_type<14>::type {aka double}' to type 'SEXP' >> >> Is there any way to make it work? >> >> Thanks, >> Rubem >> >> >> >> _______________________________________________ >> Rcpp-devel mailing list >> [email protected] >> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel > > > > _______________________________________________ > Rcpp-devel mailing list > [email protected] > https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel _______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
