[Rcpp-devel] expose functions dynamically

Dirk Eddelbuettel edd at debian.org
Mon Dec 4 17:41:59 CET 2017


On 4 December 2017 at 15:44, David Bellot wrote:
| Hi,
| 
| I'd like to dynamically add functions to a module when I call one of its
| function. Later I will want to do the same for classes too, so that to
| create classes "on the fly".
| 
| But for the moment, here is what I try to do and it doesn't work, because I
| can't see my dynamically created function in R
| 
| #include <Rcpp.h>
| 
| using namespace Rcpp;
| 
| double foobar(int x)
| {
|     return x*2.0;
| }
| 
| void makef()
| {
|     function("dynfoo",&foobar);
| }
| 
| RCPP_MODULE(test1)
| {
|     function("foobar",foobar);
|     function("makef",&makef);
| }
| 
| Then in R, I call the function makef hoping to see the function dynfoo
| appears in my global environment in R. It's not the case. I suspect a
| problem with scope (or something like that) ?
| 
| - What is the correct way to do that ?

In a package, yes. Modules require a package. Outside of a package, consider
using a package instead :)

I think a few other people / projects have invented other dynamic schemes but
I do not have a list or overview. Would be handy to have -- maybe someone
wants to blog about a comparison?

| - Can I create my functions in other environment too ?

Not sure I really understand your question.

I use Rcpp Attributes a lot, mostly via packages, and sometimes also Modules.

Dirk

-- 
http://dirk.eddelbuettel.com | @eddelbuettel | edd at debian.org


More information about the Rcpp-devel mailing list