Discussion:
[Rcpp-devel] expose functions dynamically
David Bellot
2017-12-04 15:44:10 UTC
Permalink
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 ?
- Can I create my functions in other environment too ?

Best,
David
Dirk Eddelbuettel
2017-12-04 16:41:59 UTC
Permalink
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 | ***@debian.org
David Bellot
2017-12-04 16:47:13 UTC
Permalink
Post by Dirk Eddelbuettel
| - 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 :)
​Huh ?
OK I'm lost now :-D

So in the end, how would it work ? What am I missing in my code to make it
work ?
​
Post by Dirk Eddelbuettel
​​
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?
​I'm curious too indeed.​
Tim Keitt
2017-12-04 17:36:06 UTC
Permalink
http://www.keittlab.org/
Post by Dirk Eddelbuettel
| - What is the correct way to do that ?
Post by Dirk Eddelbuettel
In a package, yes. Modules require a package. Outside of a package, consider
using a package instead :)
​Huh ?
OK I'm lost now :-D
So in the end, how would it work ? What am I missing in my code to make it
work ?
Unless I misunderstood, are you not looking for "Rcpp::sourceCpp"? Don't
use modules. Just use the // [[Rcpp::export]] attribute.

THK
Post by Dirk Eddelbuettel
​
Post by Dirk Eddelbuettel
​​
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?
​I'm curious too indeed.​
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
JJ Allaire
2017-12-04 18:10:22 UTC
Permalink
Note that you can also include modules within a C++ file and sourceCpp will
automatically add them to the calling environment (as if you had "sourced"
the definition of an R reference class).
Post by Tim Keitt
http://www.keittlab.org/
Post by Dirk Eddelbuettel
| - What is the correct way to do that ?
Post by Dirk Eddelbuettel
In a package, yes. Modules require a package. Outside of a package, consider
using a package instead :)
​Huh ?
OK I'm lost now :-D
So in the end, how would it work ? What am I missing in my code to make
it work ?
Unless I misunderstood, are you not looking for "Rcpp::sourceCpp"? Don't
use modules. Just use the // [[Rcpp::export]] attribute.
THK
Post by Dirk Eddelbuettel
​
Post by Dirk Eddelbuettel
​​
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?
​I'm curious too indeed.​
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
Continue reading on narkive:
Loading...