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

Romain Francois romain at r-enthusiasts.com
Tue May 6 09:10:25 CEST 2014


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/54f82e6d/attachment.html>


More information about the Rcpp-devel mailing list