Skip to content

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

6 messages · Romain Francois, Florian Burkart, Jerome MARQUET +2 more

#
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:

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>.

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);

Thanks for help
Florian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/3df6a6ca/attachment.html>
#
Le 6 mai 2014 ? 08:45, Florian Burkart <florian.burkart at gmail.com> a ?crit :
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) { ? }
}
This should work if you return a std::vector< TimedOptDouble > from your function, as in: 

std::vector< TimedOptDouble > GetSunPositions(SEXP a) { ? }
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/54f82e6d/attachment.html>
#
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:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/285c033d/attachment-0001.html>
#
Hello,

I am building a package "myModule" and would like to define a function 
that will be called when R is exited.

So I defined a function

    /RcppExport void R_unload_mlxComputeR(DllInfo *info)//
    //{//
    //    // Do sthg//
    //}//
    /


and I observe that it is called when dyn.unload(myModule) is called. But 
the function is not called when R is exited. I wonder if there is a 
similar function (like R_exit_packgName or R_quit_packageName) that is 
called when R is exited ?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20140506/b29ffdf5/attachment.html>
#
I believe you're looking for `.Last`. Unfortunately there's no way to
set `on.exit(...)` for the global environment.
Immediately before terminating, .Last() is executed if the function
.Last exists and runLast is true. If in interactive use there are
errors in the .Last function, control will be returned to the command
prompt, so do test the function thoroughly. There is a system
analogue, .Last.sys(), which is run after .Last() if runLast is true.

Exactly what happens at termination of an R session depends on the
platform and GUI interface in use. A typical sequence is to run
.Last() and .Last.sys() (unless runLast is false), to save the
workspace if requested (and in most cases also to save the session
history: see savehistory), then run any finalizers (see reg.finalizer)
that have been set to be run on exit, close all open graphics devices,
remove the session temporary directory and print any remaining
warnings (e.g. from .Last() and device closure).


Jamie Olson


On Tue, May 6, 2014 at 12:53 PM, Jerome MARQUET
<jerome.marquet at lixoft.net> wrote:
#
On Tue, May 6, 2014 at 11:53 AM, Jerome MARQUET
<jerome.marquet at lixoft.net>wrote:

            
A workaround would be to register a finalizer with onexit=TRUE on a dummy
object. See reg.finalizer.

THK