<div dir="ltr">Hi<br>First of all I want to say how impressed I am with Rcpp. I think it is going to be very useful for some of the packages I am developing. Thank you very much for developing it.<br><br>I have a question regarding writing custom as and wrap functions for my own classes. Following the example in:<br>
<br><a href="http://gallery.rcpp.org/articles/custom-as-and-wrap-example/">http://gallery.rcpp.org/articles/custom-as-and-wrap-example/</a><br><br>I can get my own minimal example to work with a very simple class, and using the sourceCpp() function.<br>
<br>The cpp code saved as a *.cpp file:<br><br> #include <RcppCommon.h><br><br> class DummyClass {<br> public:<br> double value;<br> };<br><br> namespace Rcpp {<br> // non-intrusive extension via template specialisation<br>
template <> DummyClass as(SEXP dt);<br> // non-intrusive extension via template specialisation<br> template <> SEXP wrap(const DummyClass &d);<br> }<br><br> #include <Rcpp.h><br>
<br> // define template specialisations for as and wrap<br> namespace Rcpp {<br> template <> DummyClass as(SEXP dtsexp) {<br> S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br> DummyClass dc;<br> dc.value = dc_s4.slot("value");<br>
return dc;<br> }<br><br> template <> SEXP wrap(const DummyClass &d) {<br> Rcpp::S4 dc_s4("DummyClass");<br> dc_s4.slot("value") = d.value;<br> return Rcpp::wrap(dc_s4);<br>
}<br> }<br><br> // [[Rcpp::export]]<br> DummyClass test_as_wrap(DummyClass dc, double multiplier){<br> DummyClass dc_out;<br> dc_out.value = dc.value * multiplier;<br> return dc_out;<br>
}<br><br><br>And the following R code compiles and calls the function:<br><br> library(Rcpp)<br> sourceCpp("DummyClass_example.cpp")<br> setClass("DummyClass", representation(value = "numeric"))<br>
dc <- new("DummyClass")<br> dc@value <- 23<br> test_as_wrap(dc, 4)<br><br>This works just fine (like magic!) and the test_as_wrap() function is happily called from R and returns an object of type DummyClass. I want to use a similar approach in a package, so I made a minimal package using:<br>
<br>Rcpp.package.skeleton("asWrapExample",attributes=TRUE)<br><br>I then split my original cpp file above into header and source code files. In the /inst/include directory I placed a file 'DummyClass_example.h' which has:<br>
<br> #include <RcppCommon.h><br><br> class DummyClass {<br> public:<br> double value;<br> };<br><br> namespace Rcpp {<br> // non-intrusive extension via template specialisation<br>
template <> DummyClass as(SEXP dt);<br> // non-intrusive extension via template specialisation<br> template <> SEXP wrap(const DummyClass &d);<br> }<br><br>In the /src directory I placed a file 'DummyClass_example.cpp' which has:<br>
<br> #include "../inst/include/DummyClass_example.h"<br> #include <Rcpp.h><br><br> // define template specialisations for as and wrap<br> namespace Rcpp {<br> template <> DummyClass as(SEXP dtsexp) {<br>
S4 dc_s4 = Rcpp::as<S4>(dtsexp);<br> DummyClass dc;<br> dc.value = dc_s4.slot("value");<br> return dc;<br> }<br><br> template <> SEXP wrap(const DummyClass &d) {<br>
Rcpp::S4 dc_s4("DummyClass");<br> dc_s4.slot("value") = d.value;<br> return Rcpp::wrap(dc_s4);<br> }<br> }<br><br> // [[Rcpp::export]]<br> DummyClass test_as_wrap(DummyClass dc, double multiplier){<br>
DummyClass dc_out;<br> dc_out.value = dc.value * multiplier;<br> return dc_out;<br> }<br><br>When I try to compile the package I get this error message:<br><br> RcppExports.cpp:9:1: error: 'DummyClass' does not name a type<br>
<br>This is probably caused by the RcppExports.cpp not having an #include for my DummyClass_example.h.<br>I understand the RcppExports.cpp file is automatically generated by the magic of Rcpp so there is no point in adding it there by hand.<br>
I've looked at the documentation but it is not clear to me how I can tell RcppExports to also include my header file (if this is the source of the problem).<br>Have I missed something in the documentation, or is there an example I can follow?<br>
Any help is appreciated.<br><br>Yours<br><br>Finlay<br><br><br><br><br></div>