在R中调用Rcpp函数

在R中调用Rcpp函数,r,rcpp,R,Rcpp,下面是引用dll中函数stl_example_model和stl_example_jacobian的R代码 library(RcppSundials) library(microbenchmark) stl_example_model = getNativeSymbolInfo(name = "example_model_stl", PACKAGE = "RcppSundials")$address stl_exa

下面是引用dll中函数stl_example_model和stl_example_jacobian的R代码

library(RcppSundials)
library(microbenchmark)

stl_example_model = getNativeSymbolInfo(name = "example_model_stl",
                                        PACKAGE = "RcppSundials")$address
stl_example_jacobian = getNativeSymbolInfo(name = "example_jacobian_stl",
                                           PACKAGE = "RcppSundials")$address

simulationstl = wrap_cvodes(times = 1:5, 
                            states_ = rep(1.0,5), 
                            parameters_ = 0.1, 
                            forcings_data_ =list(cbind(1:3600,1:3600)),
            ..., 
                            model_ = example_model, jacobian_ = example_jacobian) 
我已经使用Rcpp::sourceCpp获取了另一个函数(test.cpp)。如何使用Rcpp引用此函数(类似于stl_示例_模型)

Test.cpp

#define ARMA_DONT_USE_CXX11
#include <RcppArmadillo.h>
#include <Rcpp.h>
#include <array>
#include <vector>
using namespace std;
using namespace Rcpp;

extern "C" {
  array<vector<double>, 2> example_model_stl(const double& t, const vector<double>& states, 
            const vector<double>& parameters, const vector<double>& forcings) {

    vector<double> derivatives(states.size());  
    for(int i = 0; i < states.size(); i++) {
      derivatives[i] = -states[i]*parameters[0];
    }
    vector<double> observed{forcings[0]};
    array<vector<double>, 2> output{derivatives, observed};
    return output;
  }

  arma::mat example_jacobian_stl(const double& t, const vector<double>& states, 
            const vector<double>& parameters, const vector<double>& forcings) {
    arma::mat output = arma::eye(states.size(), states.size());
    output = -parameters[0]*output;
    return output;
  }

  array<vector<double>, 2> example_dae_stl(const double& t, const vector<double>& states, 
            const vector<double>& derivatives, const vector<double>& parameters, 
            const vector<double>& forcings) {

    vector<double> residues(states.size());  
    residues[0] = -0.04*states[0] + 1e4*states[1]*states[2];
    residues[1] = -residues[0] - 3e7*states[1]*states[1] - derivatives[1];
    residues[0] = residues[0] - derivatives[0];
    residues[2] = states[0] + states[1] + states[2] - 1.0;
    vector<double> observed{forcings[0]};
    array<vector<double>, 2> output{residues, observed};
    return output;
  }

};
#定义ARMA(不使用)
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间Rcpp;
外部“C”{
阵列示例\u模型\u stl(常数双值&t、常数向量和状态,
常数向量和参数、常数向量和作用力){
向量导数(states.size());
对于(int i=0;i
使用德克在评论中提到的包确实很有意义。然而,这并不是绝对必要的

  • getNativeSymbols
    PACKAGE
    参数实际上是可选的。如果省略,则搜索所有加载的DLL

  • 编译示例会产生警告

    在源中找不到Rcpp::导出属性或Rcpp_模块声明

    因此,生成的DLL不会加载到R的内存中。您可以通过添加导出的虚拟函数来更改此设置

  • 例如:

    /[[Rcpp::depends(“RcppArmadillo”)]]
    #定义ARMA\u不使用\u CXX11
    #包括
    #包括
    #包括
    使用名称空间std;
    使用名称空间Rcpp;
    外部“C”{
    阵列示例\u模型\u stl(常数双值&t、常数向量和状态,
    常数向量和参数、常数向量和作用力){
    向量导数(states.size());
    对于(int i=0;i
    输出:

    >Rcpp::sourceCpp('58881676.cpp')
    >getNativeSymbolInfo(“示例模型”)$address
    属性(,“类”)
    [1] “本机符号”
    

    这样,就可以将这些函数与rcppsundals一起使用。

    包处于休眠状态。我不确定使用这个包进行分析是否明智。除此之外,您知道如何从中访问代码——因为它是以包的形式存在的。因此,对代码也要这样做:将其打包。@coatless。我知道,但这真的是日晷而不是R包装。“我希望在更新日晷库时,包装纸不要破损。”@DirkEddelbuettel。我终于理解了你几次发布的内容。我用RcppArmadillo创建了一个包,效果非常好。谢谢你的帖子!这种解决方案非常有效。两条路都很慢。但这是一个小问题。
    ARMA为什么不使用CXX11
    ?这不是一个倒退的步骤,可能会导致更差的性能吗?GCC和Clang的最新版本使用C++14作为默认方言。@mtall,它只是从OP的代码中复制粘贴的。我只能假设它在那里是有原因的。