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

LEVRA-JUILLET William william.levra-juillet at addactis.com
Fri Nov 8 11:06:02 CET 2019


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 :)
Thx a mil
[cid:image005.jpg at 01D59593.3FF9BC00]<https://addactis.letsignit.com/r/32/6de77c22-c065-4906-899e-eec38189d60a/undefined>

[cid:image006.png at 01D59593.3FF9BC00]<https://addactis.letsignit.com/r/199/c042c4a7-2508-4bf6-801a-4eeb09778ade/undefined>  [cid:image007.png at 01D59593.3FF9BC00] <https://addactis.letsignit.com/r/42/b8791eba-0dab-4aa1-ab9d-813a5f8be48d/undefined>   [cid:image008.png at 01D59593.3FF9BC00] <https://addactis.letsignit.com/r/45/15ea361f-f441-4b13-94a0-4bdb30820d9a/undefined>


   William LEVRA-JUILLET
   Senior Software Architect
   ADDACTIS Software
   +33 (0)4 81 92 13 00


   www.addactis.com<https://addactis.letsignit.com/r/32/91871b88-8586-4561-b012-50a3a792527a/undefined>






[cid:image009.jpg at 01D59624.80111590]<https://addactis.letsignit.com/r/32/cf8ac9eb-d46f-4524-bc9e-5cdb532bef11/undefined>

[cid:image010.png at 01D59624.80111590]<https://addactis.letsignit.com/r/199/49e483e9-091d-4caf-a1b4-7e37d9c9ac9e/undefined>  [cid:image011.png at 01D59624.80111590] <https://addactis.letsignit.com/r/42/72f7499f-cd02-4bd4-835a-88f1053cabf2/undefined>   [cid:image012.png at 01D59624.80111590] <https://addactis.letsignit.com/r/45/b9ce1c50-1455-48cb-b90f-b8348d799476/undefined>


   William LEVRA-JUILLET
   Senior Software Architect
   ADDACTIS Software
   +33 (0)4 81 92 13 00


   www.addactis.com<https://addactis.letsignit.com/r/32/9cd210c2-e7cd-4d30-995a-e3ba4da16780/undefined>





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image005.jpg
Type: image/jpeg
Size: 2629 bytes
Desc: image005.jpg
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0002.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image006.png
Type: image/png
Size: 1454 bytes
Desc: image006.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0006.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image007.png
Type: image/png
Size: 1470 bytes
Desc: image007.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0007.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image008.png
Type: image/png
Size: 1571 bytes
Desc: image008.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0008.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image009.jpg
Type: image/jpeg
Size: 2629 bytes
Desc: image009.jpg
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0003.jpg>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image010.png
Type: image/png
Size: 1455 bytes
Desc: image010.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0009.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image011.png
Type: image/png
Size: 1470 bytes
Desc: image011.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0010.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image012.png
Type: image/png
Size: 1573 bytes
Desc: image012.png
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20191108/4de05990/attachment-0011.png>


More information about the Rcpp-devel mailing list