[Rcpp-devel] inline with classes

Whit Armstrong armstrong.whit at gmail.com
Tue Oct 5 22:38:10 CEST 2010


Thanks for the example!  that makes everything clear.

-Whit


2010/10/5 Dirk Eddelbuettel <edd at debian.org>:
>
> On 5 October 2010 at 13:21, Whit Armstrong wrote:
> | aha, thanks for correcting me. on the inline/Rcpp division of labor.
> |
> | So, what you are suggesting should have been obvious to me.
> |
> | I pass the class def in the include statement of the inline function?
> | Is that it?
> |
> | I'll ping back after I try this.
> |
> | If this really works, then I think WinBUGS can finally die.
>
> It works over here, chances are you may get lucky too.
>
> Here, I took a simple templated class square from one of the unit tests and
> wrapped it in (fairly verbose) example:
>
>
> edd at max:~$ cat /tmp/whit.r
> #!/usr/bin/r -ti
>
> suppressMessages(library(inline))
> suppressMessages(library(Rcpp))
>
> inc <- 'template <typename T>
>        class square : public std::unary_function<T,T> {
>        public:
>            T operator()( T t) const { return t*t ;}
>        };
>       '
>
> src <- '
>       double x = Rcpp::as<double>(xs);
>       int i = Rcpp::as<int>(is);
>       square<double> sqdbl;
>       square<int> sqint;
>       return Rcpp::List::create(Rcpp::Named("x", sqdbl(x)),
>                                 Rcpp::Named("i", sqint(i)));
>       '
> fun <- cxxfunction(signature(xs="numeric", is="integer"), body=src, include=inc, plugin="Rcpp")
>
> print(fun(2.0, 3L))
> edd at max:~$ chmod 0755 /tmp/whit.r
> edd at max:~$ /tmp/whit.r
> $x
> [1] 4
>
> $i
> [1] 9
>
> edd at max:~$
>
>
> All good?  Send my condolences to the family of WinBUGS.
>
> Dirk
>
>
>
>
> | -Whit
> |
> |
> | 2010/10/5 Dirk Eddelbuettel <edd at debian.org>:
> | >
> | > Hi Whit,
> | >
> | > On 5 October 2010 at 12:25, Whit Armstrong wrote:
> | > | I just reviewed the Rcpp documentation.
> | >
> | > Great. Now tell us how to make it sticky so that we get you to contribute :)
> | >
> | > | I see plenty of cfunction/cxxfunction examples, but I'm curious
> | > | whether one can provide a class definition inline in an R script and
> | > | then initialize an instance of the class and call a method on the
> | > | class.  all inline in R.
> | > |
> | > | Is this feature something you all would consider adding to Rcpp?
> | >
> | > a) A change would be an inline feature, not an Rcpp feature.
> | >
> | > b) But we already do :) support this, and even use it in numerous cases in
> | >   the unit tests; this uses the   include=   argument to cxxfunction et al.
> | >
> | > So you can supply your templated logic in an R string to include= and then
> | > use the code in an R string to to src= and pass it all through cxxfunction()
> | > for a quick test.
> | >
> | > See e.g. runit.macros.R or or runit.Module.R.  I think we also have blog
> | > posts and / or vignette examples that use it.
> | >
> | > Hth, Dirk
> | >
> | > --
> | > Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
> | >
>
> --
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com
>


More information about the Rcpp-devel mailing list