[Rcpp-devel] RInside: C/C++ function(s) passed to R?

Iñaki Ucar iucar at fedoraproject.org
Fri Nov 8 11:15:54 CET 2019


There's an article about this in the Rcpp Gallery [1], but it seems
that some commit dropped half of the article (?). Anyway, there's a
cached version in [2], and you can always go directly to the Rmd [3].

[1] https://gallery.rcpp.org/articles/passing-cpp-function-pointers-rcppxptrutils/
[2] https://web.archive.org/web/20171029135311/https://gallery.rcpp.org/articles/passing-cpp-function-pointers-rcppxptrutils/
[3] https://github.com/RcppCore/rcpp-gallery/blob/gh-pages/src/2017-08-04-passing-cpp-function-pointers-rcppxptrutils.Rmd

Iñaki


On Fri, 8 Nov 2019 at 11:06, LEVRA-JUILLET William
<william.levra-juillet at addactis.com> wrote:
>
> Hi all,
>
>
>
> I’m evaluating RInside/Rcpp to interact our C++ project with R.
>
> I’ve seen the samples to get/set data (matrix, dataframes, etc) to/from R and it builds/works fine.
>
> (Using gcc/mingw compiler from RTools on Windows)
>
>
>
> My questions is about functions…
>
> My project is C++, and it uses RInside to call R from our code.
>
> Let’s say I have a C++ function implemented in my project, and I want to pass that c++ function to R so it can be “used” as a R function (typically passed as argument to optim/optimize)
>
> I couldn’t find a way to do that using RInside.
>
>
>
> Ideally, I’d like to achieve something like:
>
>
>
> #include <RInside.h>
>
>
>
> double Banana(double x, double y)
>
> {
>
> return (1-x)*(1-x)+100*(y-x*x)*(y-x*x);
>
> }
>
>
>
> Int main()
>
> {
>
> RInside R(argc,, argv);
>
> R[“fn”] = Banana;
>
> R.parseEval(“optim(c(-2,-2), fn”);
>
> }
>
>
>
> I believe it’s similar to https://stackoverflow.com/questions/53729493/rinside-wrapping-a-c-function-for-use-in-an-r-function
>
> But I can’t really understand the Rcpp::XPtr suggestion from Dirk.
>
> Any help or pointers welcome J
>
> Thx a mil
>
>
>
>
>
>
>    William LEVRA-JUILLET
>    Senior Software Architect
>    ADDACTIS Software
>    +33 (0)4 81 92 13 00
>
>
>    www.addactis.com
>
>
>
>
>
>
>
>
>
>
>
>
>
>    William LEVRA-JUILLET
>    Senior Software Architect
>    ADDACTIS Software
>    +33 (0)4 81 92 13 00
>
>
>    www.addactis.com
>
>
>
>
>
>
> _______________________________________________
> 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



-- 
Iñaki Úcar


More information about the Rcpp-devel mailing list