Skip to content

[Rcpp-devel] expose functions dynamically

5 messages · Dirk Eddelbuettel, David Bellot, Tim Keitt +1 more

#
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20171204/4da53ed4/attachment.html>
#
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
#
?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 ?
?
?I'm curious too indeed.?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20171204/6d1789dc/attachment.html>
#
http://www.keittlab.org/

On Mon, Dec 4, 2017 at 10:47 AM, David Bellot <david.bellot at gmail.com>
wrote:
Unless I misunderstood, are you not looking for "Rcpp::sourceCpp"? Don't
use modules. Just use the // [[Rcpp::export]] attribute.

THK
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20171204/e30b0c45/attachment.html>
#
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).
On Mon, Dec 4, 2017 at 12:36 PM, Tim Keitt <tkeitt at utexas.edu> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20171204/aaa86f7a/attachment-0001.html>