Discussion:
[Rcpp-devel] Modules + Attributes for extensions
f***@gmail.com
2018-03-05 23:08:55 UTC
Permalink
Dear All,

I working in an R package where the main feature is some classes
exposed through Modules. Besides, the classes constructors and methods,
I am also exporting some functions that take classes instances plus
other parameters to return new classes instances. So far, so good,
everything is working pretty well. 

However, I also would like to share a C++ interface to perspective
users so, they can extend the package with new features that depends on
the classes definitions.

Unfortunately, if toghether with the Module, we have defined a function
that takes as argument classes instances and exporting it through
attributes, the compilation crashes. The error is the same as if after
building, installing and loading and then using sourceCpp the user
inserts the depends attribute for the package, includes the package
header and try by herself new features.

Based on the log I can figure out that it complains by the fact that
there is no as/wrap defined. 

Anyone can give advice on how to circunvent this? The Rcpp_Exports has
always the funtions exported by the attributes first than the module
even if they are in reverse order in the same file. Is it a clue for a
solution?

I have just put a ToyPack that reproduce this error (https://github.com
/FHToledo/ToyPack). In src/Interface.cpp there is a commented chunck
that causes the following error:

[When Building]

...

/usr/local/lib/R/site-library/Rcpp/include/Rcpp/internal/Exporter.h: In
instantiation of ‘Rcpp::traits::Exporter<T>::Exporter(SEXP) [with T =
CC; SEXP = SEXPREC*]’:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:87:51:   required
from ‘T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag)
[with T = CC; SEXP = SEXPREC*]’
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:152:31:   required
from ‘T Rcpp::as(SEXP) [with T = CC; SEXP = SEXPREC*]’
/usr/local/lib/R/site-
library/Rcpp/include/Rcpp/InputParameter.h:34:43:   required from
‘Rcpp::InputParameter<T>::operator T() [with T = CC]’
RcppExports.cpp:19:59:   required from here
/usr/local/lib/R/site-
library/Rcpp/include/Rcpp/internal/Exporter.h:31:31: error: no matching
function for call to ‘CC::CC(SEXPREC*&)’
       Exporter( SEXP x ) : t(x){}

...

[When Extending]

library(ToyPack)
library(Rcpp)

i <- 10
j <- 20
x <- 2
y <- 2.5

AA <- CA$new(i, j)
BB <- CB$new(x, y)
C1 <- CC$new(AA, BB)
DD <- increase_c(C1, AA, 2, 5.5)

src <- '
// [[Rcpp::depends(ToyPack)]]

# include <cmath>
# include <ToyPack.h>

CC evolve_c(CC old, int minus, double plus) {

  old.a.process(minus) ;
  
  int w(old.a.size) ;
  int z(old.b.get_size() + w) ;
  
  return CC(CA(w, 0), CB(z, M_PI)) ;

}
'

sourceCpp(code = src)

...

/usr/local/lib/R/site-library/Rcpp/include/Rcpp/internal/Exporter.h: In
instantiation of ‘Rcpp::traits::Exporter<T>::Exporter(SEXP) [with T =
CC; SEXP = SEXPREC*]’:
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:87:51:   required
from ‘T Rcpp::internal::as(SEXP, Rcpp::traits::r_type_generic_tag)
[with T = CC; SEXP = SEXPREC*]’
/usr/local/lib/R/site-library/Rcpp/include/Rcpp/as.h:152:31:   required
from ‘T Rcpp::as(SEXP) [with T = CC; SEXP = SEXPREC*]’
/usr/local/lib/R/site-
library/Rcpp/include/Rcpp/InputParameter.h:34:43:   required from
‘Rcpp::InputParameter<T>::operator T() [with T = CC]’
file632c6668acf5.cpp:31:59:   required from here
/usr/local/lib/R/site-
library/Rcpp/include/Rcpp/internal/Exporter.h:31:31: error: no matching
function for call to ‘CC::CC(SEXPREC*&)’
       Exporter( SEXP x ) : t(x){}
       
...

Any help, advice, comment and/or feedback will be extremely helpful. I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!

Thanks in advance,
FH
Ralf Stubner
2018-03-06 13:41:32 UTC
Permalink
Post by f***@gmail.com
I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!
The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pdf
Also very useful:
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seamingless-interfaces/

Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL

Greetings
Ralf
--
Ralf Stubner
Senior Software Engineer / Trainer

R Institute GmbH
Dortustraße 48
14467 Potsdam

T: +49 331 23 70 81 66
F: +49 331 23 70 81 67
M: +49 162 20 91 196

Mail: ***@r-institute.com

Sitz: Potsdam
Register: AG Potsdam HRB 27966 P
Ust.-IdNr.: DE300072622
GeschÀftsfÌhrer: Prof. Dr. Dr. Karl-Kuno Kunze
f***@gmail.com
2018-03-07 14:59:46 UTC
Permalink
Dear Ralf and others,

Thanks for your reply. I did looked for the references you pointed
before. Unfortunately, there is nothing new in the Extending vignette
than what can be found in Dirk's book chapter. Basically, all the
others implement custom as/wrap for third part classes to SEXP
fundamental types.

Perhaps, I didn't state the question clearly... : Given my own class
exposed through Modules, how to retrieve it from the C++ side? How to
allow perspective users to extend a package taking exposed class and
defining new Rccp functions that uses those exposed class as argument
or return?

My main difficulty is that! I will appreciate your points over that?

Cheers,
FH

-----Original Message-----
From: Ralf Stubner <***@r-institute.com>
To: rcpp-***@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Tue, 6 Mar 2018 14:41:32 +0100
Post by f***@gmail.com
I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!
The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pd
f
Also very useful:
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seami
ngless-interfaces/

Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL

Greetings
Ralf
Serguei Sokol
2018-03-07 15:57:20 UTC
Permalink
Post by f***@gmail.com
Dear Ralf and others,
Thanks for your reply. I did looked for the references you pointed
before. Unfortunately, there is nothing new in the Extending vignette
than what can be found in Dirk's book chapter. Basically, all the
others implement custom as/wrap for third part classes to SEXP
fundamental types.
Perhaps, I didn't state the question clearly... : Given my own class
exposed through Modules, how to retrieve it from the C++ side?
(Supposing I understood well your question...)

For this part of problem, you have to declare your classes in your
original package (say mypack) in a separate file say mypack/inst/include/mypack.h
Then in a package which will extend it (say otherpack) you will
delcare in its DESCRIPTION
LinkingTo: mypack

It will allow you to put in your otherpack/src/*.cpp files
#include <mypack.h> So you'll be able to use/extend mypack classes.
Post by f***@gmail.com
How to
allow perspective users to extend a package taking exposed class and
defining new Rccp functions that uses those exposed class as argument
or return?
As to taking objects instantiated from your classes as arguments in R session,
it's indeed a matter of custom as()/wrap() custom definition.
Or you can use XPtr hook to passe just pointers to your objects
and de-referencing them appropriately in the Rcpp code.

Best,
Serguei.
Post by f***@gmail.com
My main difficulty is that! I will appreciate your points over that?
Cheers,
FH
-----Original Message-----
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Tue, 6 Mar 2018 14:41:32 +0100
Post by f***@gmail.com
I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!
The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.pd
f
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-seami
ngless-interfaces/
Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL
Greetings
Ralf
_______________________________________________
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
f***@gmail.com
2018-03-07 18:02:34 UTC
Permalink
Dear Sergei,

You got my question... however:

I followed what is shown in RcppBDT as well as in Rcpp Extending
Vignette i.e., include RcppCommon.h, define the class, include Rcpp.h.
But, how the implement as/wrap? 

I also turn available the extensions trough the "depends" attribute. It
works and recognize my class...

What I can't understand (implement) is how to define an as<MyClass>
taking MyClass as input from R as well as the wrap(MyClass) which is
supposed to return an instance of MyClass to R.

I did try the Rcpp::XPtr approach for wrapping too but, then what I get
in the R side is and externalptr, not an instance of MyClass.

Any clue or example to point me? I will appreciate :)

Cheers,
FH

-----Original Message-----
From: Serguei Sokol <***@gmail.com>
Reply-to: ***@gmail.com
To: ***@gmail.com, rcpp-***@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Wed, 7 Mar 2018 16:57:20 +0100
Post by f***@gmail.com
Dear Ralf and others,
Thanks for your reply. I did looked for the references you pointed
before. Unfortunately, there is nothing new in the Extending vignette
than what can be found in Dirk's book chapter. Basically, all the
others implement custom as/wrap for third part classes to SEXP
fundamental types.
Perhaps, I didn't state the question clearly... : Given my own class
exposed through Modules, how to retrieve it from the C++ side?
(Supposing I understood well your question...)

For this part of problem, you have to declare your classes in your
original package (say mypack) in a separate file say
mypack/inst/include/mypack.h
Then in a package which will extend it (say otherpack) you will
delcare in its DESCRIPTION
LinkingTo: mypack

It will allow you to put in your otherpack/src/*.cpp files
#include <mypack.h> So you'll be able to use/extend mypack classes.
Post by f***@gmail.com
How to
allow perspective users to extend a package taking exposed class and
defining new Rccp functions that uses those exposed class as argument
or return?
As to taking objects instantiated from your classes as arguments in R
session,
it's indeed a matter of custom as()/wrap() custom definition.
Or you can use XPtr hook to passe just pointers to your objects
and de-referencing them appropriately in the Rcpp code.

Best,
Serguei.
Post by f***@gmail.com
My main difficulty is that! I will appreciate your points over that?
Cheers,
FH
-----Original Message-----
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Tue, 6 Mar 2018 14:41:32 +0100
Post by f***@gmail.com
I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!
The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.
pd
f
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-sea
mi
ngless-interfaces/
Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL
Greetings
Ralf
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-dev
el
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-dev
el
f***@gmail.com
2018-03-09 04:02:44 UTC
Permalink
Dear All,

So, after some more digging on the web and studying pointed packages
and documents I did achieve my goal. For those who may have the same
needs, I suggest you take a look on how RcppR6 deal and propose.
Anyway, many thanks to Ralf and Sergei.

Cheers,
FH

-----Original Message-----
From: ***@gmail.com
To: rcpp-***@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Wed, 07 Mar 2018 12:02:34 -0600

Dear Sergei,

You got my question... however:

I followed what is shown in RcppBDT as well as in Rcpp Extending
Vignette i.e., include RcppCommon.h, define the class, include Rcpp.h.
But, how the implement as/wrap? 

I also turn available the extensions trough the "depends" attribute. It
works and recognize my class...

What I can't understand (implement) is how to define an as<MyClass>
taking MyClass as input from R as well as the wrap(MyClass) which is
supposed to return an instance of MyClass to R.

I did try the Rcpp::XPtr approach for wrapping too but, then what I get
in the R side is and externalptr, not an instance of MyClass.

Any clue or example to point me? I will appreciate :)

Cheers,
FH

-----Original Message-----
From: Serguei Sokol <***@gmail.com>
Reply-to: ***@gmail.com
To: ***@gmail.com, rcpp-***@lists.r-forge.r-project.org
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Wed, 7 Mar 2018 16:57:20 +0100
Post by f***@gmail.com
Dear Ralf and others,
Thanks for your reply. I did looked for the references you pointed
before. Unfortunately, there is nothing new in the Extending vignette
than what can be found in Dirk's book chapter. Basically, all the
others implement custom as/wrap for third part classes to SEXP
fundamental types.
Perhaps, I didn't state the question clearly... : Given my own class
exposed through Modules, how to retrieve it from the C++ side?
(Supposing I understood well your question...)

For this part of problem, you have to declare your classes in your
original package (say mypack) in a separate file say
mypack/inst/include/mypack.h
Then in a package which will extend it (say otherpack) you will
delcare in its DESCRIPTION
LinkingTo: mypack

It will allow you to put in your otherpack/src/*.cpp files
#include <mypack.h> So you'll be able to use/extend mypack classes.
Post by f***@gmail.com
How to
allow perspective users to extend a package taking exposed class and
defining new Rccp functions that uses those exposed class as argument
or return?
As to taking objects instantiated from your classes as arguments in R
session,
it's indeed a matter of custom as()/wrap() custom definition.
Or you can use XPtr hook to passe just pointers to your objects
and de-referencing them appropriately in the Rcpp code.

Best,
Serguei.
Post by f***@gmail.com
My main difficulty is that! I will appreciate your points over that?
Cheers,
FH
-----Original Message-----
Subject: Re: [Rcpp-devel] Modules + Attributes for extensions
Date: Tue, 6 Mar 2018 14:41:32 +0100
Post by f***@gmail.com
I
will also appreciate if someone point me an R package in which custom
as/wrap are defined!
The reference: http://dirk.eddelbuettel.com/code/rcpp/Rcpp-extending.
pd
f
http://gallery.rcpp.org/articles/custom-templated-wrap-and-as-for-sea
mi
ngless-interfaces/
Simple example: RcppBDT
More complex examples: RcppArmadillo, RcppEigen, RcppGSL
Greetings
Ralf
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-dev
el
_______________________________________________
Rcpp-devel mailing list
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-dev
el
Loading...