简体   繁体   English

用Rcpp公开C ++类

[英]Exposing C++ class with Rcpp

I've been playing around with Rcpp and a couple of questions are currently popping up... 我一直在玩Rcpp,目前正在出现一些问题......

From my understanding, if you want to expose a C++ class to R you need to write partial template specializations for Rcpp::wrap and Rcpp::as. 根据我的理解,如果要将C ++类公开给R,则需要为Rcpp :: wrap和Rcpp :: as编写部分模板特化。 I looked how this was done in the Rcpp::Date class and I have the following questions: - In Date.h we have: 我看了这是如何在Rcpp :: Date类中完成的,我有以下问题: - 在Date.h中我们有:

// 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 down the header 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 is the wrap_extra_steps supposed to do? wrap_extra_steps应该做什么? Is it required? 需要吗? Also in Date.cpp wrap method is implemented as follows: 另外在Date.cpp中的wrap方法实现如下:

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

With the internal::new_date_object implemented as: 内部:: new_date_object实现为:

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 an SEXP is created and returned to R, but I don't get the whole part with PROTECT(), Rf_setAttrib, UNPROTECT...what's happening here? 好的我明白SEXP是创建并返回给R的,但是我没有完全了解PROTECT(),Rf_setAttrib,UNPROTECT ......这里发生了什么?

Thanks! 谢谢!

There is an entire vignette discussing how to write as<>() and wrap() ---the Rcpp-extending vignette. 有一个完整的小插图讨论如何写as<>()wrap() --- Rcpp扩展的插图。

As it discusses, partial specialization is just one of three approaches, and there are other example packages too. 正如它所讨论的,部分专业化只是三种方法之一,也有其他示例包。 Date() is something Rcpp itself implements, so it is not the best example. Date()是Rcpp本身实现的东西,所以它不是最好的例子。 Read the vignette, study other examples and ask on rcpp-devel. 阅读小插图,研究其他示例并询问rcpp-devel。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM