[Rcpp-devel] Questions on extending Rcpp wrap and as with templates

Florian Burkart florian.burkart at gmail.com
Tue May 6 17:09:20 CEST 2014


Hi Romain,

You are correct.

I have changed to specializations as below.

However, as to the return type, are you saying the header file then also
becomes

RcppExport std::vector< TimedOptDouble > GetSunPositions(SEXP a)

instead of

RcppExport SEXP GetSunPositions(SEXP a)

? Or do I leave the header unchanged and just change the return type of the
implementation?


In terms of the second point, I use OptDouble a lot (e.g.
boost::optional<double>), which converts to a double with

double ToDouble(OptDouble const& in) {
return in ? *in : R_NaReal;
}

unfortunately that means that all the nice semantics available to me in
Rcpp, e.g. Named("Results")=std::list, or std::vector, or
std::vector<std::vector > > have all become unavailable as I always use
OptDouble instead of double.

Is it really that much hassle for me to add boost::optional<double> to the
internal handling?

Thank you
Florian


On Tue, May 6, 2014 at 9:10 AM, Romain Francois <romain at r-enthusiasts.com>wrote:

>
> Le 6 mai 2014 à 08:45, Florian Burkart <florian.burkart at gmail.com> a
> écrit :
>
> Hi everyone (and Dirk),
>
> Second attempt on corrected email list.
>
> I have been trying to extend Rcpp with my own wrap and as templates.
>
> Two issues:
>
> 1) I need to explicitly call wrap. Is that expected?
>
> So for example I wrote this specialization:
>
>
> This is not a specialization, just another overload. You need to write a
> specialization, as in:
>
> namespace Rcpp{
> template<> SEXP wrap<std::vector< TimedOptDouble > >
> (std::vector<TimedOptDouble> const& entries) { … }
> }
>
> template<> SEXP Rcpp::wrap(std::vector<TimedOptDouble> const& entries) {
>  std::vector<double> sec_times;
> std::vector<double> doubles;
> for(auto const& entry : entries)
>  {
> sec_times.push_back(entry.GetTime().Seconds());
> TimedOptDouble::OptDouble opt_double(entry.GetOptDouble());
>  if(opt_double)
> doubles.push_back(*opt_double);
> else
> doubles.push_back(R_NaReal);
>  }
> return List::create(
> Named( "Time" ) = sec_times,
>  Named( "Value" ) = doubles);
> }
>
> First of all, this returns what I believe to be a Rcpp::List object, which
> seems to be converted implicitly to a SEXP. This is the typical behaviour I
> know.
>
>
> Unfortunately, when making use of this template, it doesn't work
> implicitly, but I need to explicitly call it.
>
> So for example
>
> SEXP GetSunPositions(SEXP a) {
>  std::vector<TimedOptDouble> sun_positions;
>  ...
> return wrap(sun_positions);
> }
>
> works, where as
>
> return sun_positions;
>
> as last line doesn't. Am I doing something wrong here? I did do the
> declaration before including <Rcpp.h>.
>
>
> This should work if you return a std::vector< TimedOptDouble > from your
> function, as in:
>
> std::vector< TimedOptDouble > GetSunPositions(SEXP a) { … }
>
> 2) How to make as work for own types in containers
>
> The other way around, one can return a std::vector<double> implicitly, but
> how do I return std::vector<MyType>? I tried to define
>
> template<> MyType as(SEXP);
>
> But that didn't help, e.g. I had to write my own
>
> template<> std::vector<MyType> as(SEXP);
>
>
> This is the easiest way. A more general way would need you to express how
> to handle containers of MyType, but that requires defining some traits
> classes etc … not sure it is worth the effort.
>
> But again, you’d need to actually write a specialization: template<>
> std::vector<MyType> as< std::vector<MyType> > (SEXP);
>
> Romain
>
>
> Thanks for help
> Florian
>  _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/285c033d/attachment-0001.html>


More information about the Rcpp-devel mailing list