Tag: rcpp

‘unlockEnvironment’通过’Rcpp’而不是’inline’实现

实际问题 有人可以让我开始我需要做什么来实现unlockEnvironment下面的unlockEnvironment代码? 背景 看过这篇文章 ,尝试了基于C代码的Winston Chang的解决方案。 它有效,但我觉得我知道太少(实际上没什么,都是)关于内联或C / C ++来真正知道我在做什么;-) 所以我认为这将是一个很好的机会,最终开始学习如何使用R作为C和C ++的接口。 而且我想我会跳上Rcpp火车这样做! Winston’s Gist的代码 require(“inline”) inc <- ' /* This is taken from envir.c in the R 2.15.1 source https://github.com/SurajGupta/r-source/blob/master/src/main/envir.c */ #define FRAME_LOCK_MASK (1<<14) #define FRAME_IS_LOCKED(e) (ENVFLAGS(e) & FRAME_LOCK_MASK) #define UNLOCK_FRAME(e) SET_ENVFLAGS(e, ENVFLAGS(e) & (~ FRAME_LOCK_MASK)) ' src <- ' if (TYPEOF(env) == NILSXP) […]

Rcpp:将C数组作为NumericMatrix返回到R

#include #include extern “C” { #include “cheader.h” } using namespace Rcpp; // [[Rcpp::export]] NumericVector cppfunction(NumericVector inputR){ double const* input = inputR.begin(); size_t N = inputR.size(); double output[10*N]; cfunction(input, N, output); std::vector outputR(output, output + sizeof(output) / sizeof(double)); return wrap(outputR); } 这是有效的,除了我必须手动将矢量outputR转换为R中的矩阵。我当然也可以使outputR到NumericMatrix(或者我可以?)然后返回,但我真正的问题是上述过程是最优的吗? 我是否必须先将输出转换为std :: vector,然后再转换为NumericVector / Matrix,还是可以以某种方式避免这种情况? 我试着直接包装输出但是没有用。

使用Rcpp中其他包的C函数

我正在尝试从c ++函数中的cubature包调用C例程来执行多维集成。 我试图重现的基本R例子是 library(cubature) integrand <- function(x) sin(x) adaptIntegrate(integrand, 0, pi) 我可以从图库中按照这个配方从Rcpp调用这个R函数,但是从c / c ++到R来回切换会有一些性能损失。从C ++直接调用C函数似乎更合理。 C例程adapt_integrate从adapt_integrate导出 // R_RegisterCCallable(“cubature”, “adapt_integrate”, (DL_FUNC) adapt_integrate); 但是,我不明白如何从c ++中调用它。 这是我的跛脚尝试, sourceCpp(code = ‘ #include using namespace Rcpp; // [[Rcpp::export]] double integrand(double x){ return(sin(x)); } // [[Rcpp::depends(cubature)]] // [[Rcpp::export]] Rcpp::List integratecpp(double llim, double ulim) { Rcpp::Function p_cubature = R_GetCCallable(“cubature”, “adapt_integrate”); Rcpp::List […]