小编sky*_*eer的帖子

通过环境Rcpp从c ++调用函数

我正在考虑通过环境从c ++调用R函数,但是我得到了一个错误,这就是我所做的

#include <Rcpp.h>
using namespace Rcpp;



// [[Rcpp::export]]
NumericVector call(NumericVector x){
  Environment env = Environment::global_env();
  Function f = env["fivenum"];
  NumericVector res = f(x);
  return res;
}
Run Code Online (Sandbox Code Playgroud)

打字call(x),这是我得到的,

Error: cannot convert to function
Run Code Online (Sandbox Code Playgroud)

我知道我可以用另一种方式做到

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector callFunction(NumericVector x, Function f) {
    NumericVector res = f(x);
    return res;
}
Run Code Online (Sandbox Code Playgroud)

并输入

callFunction(x,fivenum)
Run Code Online (Sandbox Code Playgroud)

但仍然想知道为什么第一种方法失败.

r rcpp

4
推荐指数
1
解决办法
1179
查看次数

RCPP无法在初始化时将“ SEXP {aka SEXPREC *}”转换为“ double”

我正在尝试在Rcpp中复制R向量化的总和

我首先尝试以下无故障代码:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(NumericVector x){
  return sum(x);
}
Run Code Online (Sandbox Code Playgroud)

类型 call(Time)

> call(Time)
[1] 1919853
Run Code Online (Sandbox Code Playgroud)

然后是环境版本,也很好用,

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(){
  Environment env = Environment::global_env();
  NumericVector Time = env["Time"];
  return sum(Time);
}
Run Code Online (Sandbox Code Playgroud)

类型 call()

> call()
[1] 1919853
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试以下奇怪的事情,

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(){
  Environment base("package:base");
  Function sumc = base["sum"];
  Environment env = Environment::global_env();
  NumericVector Time = env["Time"];
  double res = sumc(Time);
  return res;
}
Run Code Online (Sandbox Code Playgroud)

这次我收到一条错误消息: …

r rcpp

1
推荐指数
1
解决办法
1467
查看次数

标签 统计

r ×2

rcpp ×2