Revealing C ++ Class Using Rcpp

I was playing with Rcpp, and currently there are a few questions ...

From my point of view, if you want to open a C ++ class for R, you need to write partial specialized templates for Rcpp :: wrap and Rcpp :: as. I looked at how this was done in the Rcpp :: Date class, and I have the following questions: - In Date.h we have:

// template specialisation for wrap() on the date // OK as explained in docs for non intrusive // partial template specialization template <> SEXP wrap<Rcpp::Date>(const Rcpp::Date &date); 

Further on the heading you have the following code:

 template<> inline SEXP wrap_extra_steps<Rcpp::Date>( SEXP x ){ Rf_setAttrib( x, R_ClassSymbol, Rf_mkString( "Date" ) ) ; return x ; } 

What should wrap_extra_steps do? It's necessary? The following is also implemented in the Date.cpp wrapping method:

 template <> SEXP wrap(const Date &date) { return internal::new_date_object( date.getDate() ) ; } 

With internal :: new_date_object implemented as:

 SEXP new_date_object( double d){ SEXP x = PROTECT(Rf_ScalarReal( d ) ) ; Rf_setAttrib(x, R_ClassSymbol, Rf_mkString("Date")); UNPROTECT(1); return x; } 

OK. I understand that SEXP is created and returned in R, but I do not get the whole part with PROTECT (), Rf_setAttrib, UNPROTECT ... what happens here?

Thanks!

+4
source share
1 answer

There is a whole vignette that discusses how to write as<>() and wrap() --- a vignette that extends Rcpp.

As he discusses, partial specialization is just one of three approaches, and there are other sample packages. Date() is what Rcpp implements, so this is not a good example. Read the vignette, explore other examples, and ask on rcpp-devel.

+1
source

Source: https://habr.com/ru/post/1480228/


All Articles