在`R`中使用`c ++`类模板 [英] Using `c++` class templates into `R`

查看:51
本文介绍了在`R`中使用`c ++`类模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次查看如何编写自动换行功能,使我可以与在c ++中创建的类模板进行交互,但我有几个问题。

It's my first time taking a look at how to write wrap functions that allow me to interface with a class template I have created in c++, and I have a few questions.

如何做我使用 Rcpp :: depends 属性声明对我创建的静态库的依赖吗?我假设我需要在某处指定路径,但是这里的所有示例仅引用众所周知的库。

How do I use an Rcpp::depends attribute to declare dependencies on static libraries that I've created? I assume I'll need to specify a path somewhere, but all the examples here just refer to libraries that are well known.

<$ c $的选项是什么? c> R 类型,当我尝试在 R 会话中使用这些东西时可以使用吗?我有 c ++ 类,它们不仅包含一些数据,而且还具有处理该数据并更改状态的方法。这个例子似乎只是将vector变成另一个。有人对如何实现此目标有任何示例或建议吗?

What are my options for R types I can use when I try to use this stuff in an R session? I have c++ classes that not only contain some data, but they also have methods that process this data and change state. This example only seems to change on vector into another. Does anyone have any examples or suggestions on how this can be accomplished?

namespace Rcpp {

    namespace traits{

        // Defined wrap case
        template <typename T> SEXP wrap(const boost::numeric::ublas::vector<T> & obj){
            const int RTYPE = Rcpp::traits::r_sexptype_traits<T>::rtype ;

            return Rcpp::Vector< RTYPE >(obj.begin(), obj.end());
        };


    }
}


推荐答案

这是一个非常广泛的问题,所以我只能提供一些可能对您有所帮助的指针:

This is a very broad question, so I can only give some pointers that might help you:


  • Rcpp :: depends 属性不是要依赖于外部库,而是要依赖其他R包。这些R软件包可能会提供库(例如RcppArmadillo或RcppEigen)或与系统库链接的必要基础结构(例如RcppGSL)。后者是通过插件c.f实现的。 RcppGSL :: inlineCxxPlugin()。插件也可以不带额外的包装使用,请参见c.f。 此处

  • The Rcpp::depends attribute is not meant for dependencies on external libraries but for other R packages. Those R packages might provide the libraries (e.g. RcppArmadillo or RcppEigen) or the necessary infrastructure to link with a system library (e.g. RcppGSL). The latter is achieved via plugins, c.f. RcppGSL::inlineCxxPlugin(). Plugins can also be used without an extra package, c.f. here.

如果要维护C ++对象内的状态,则必须考虑其生命周期。如果它是一个单例对象,则我喜欢使用匿名名称空间。否则,您可以使用 Rcpp :: XPtr 将C ++对象带到R。这也可以使用 RCPP_EXPOSED_CLASS 来实现。宏,比照

If you want to maintain state within a C++ object, you have to consider its life-cycle. If it a singleton like object I like to use an anonymous namespace. Otherwise you can use Rcpp::XPtr to bring the C++ object to R. This can also be achieved using the RCPP_EXPOSED_CLASS macro, c.f. the "Extending Rcpp" vignette.

Rcpp模块也可能值得一看。有关示例,请参见相应的小插图和RcppRedis软件包。

Rcpp modules might also be worth a look. See the corresponding vignette and the RcppRedis package for an example.

这篇关于在`R`中使用`c ++`类模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆