Rcpp无法转换‘;SEXP{aka SEXPREC*}’;至‘;双人床’;在初始化中

Rcpp无法转换‘;SEXP{aka SEXPREC*}’;至‘;双人床’;在初始化中,r,rcpp,R,Rcpp,我试图在Rcpp中复制R矢量化和 我首先尝试以下无故障代码: #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double call(NumericVector x){ return sum(x); } 然后一个环境版本,也很好用 #include <Rcpp.h> using namespace Rcpp; // [[Rcpp::export]] double call(){ Envi

我试图在Rcpp中复制R矢量化和

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

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(NumericVector x){
  return sum(x);
}
然后一个环境版本,也很好用

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(){
  Environment env = Environment::global_env();
  NumericVector Time = env["Time"];
  return sum(Time);
}
现在我正在做一些奇怪的事情如下

#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;
}

知道怎么回事吗

不能对某个Rcpp向量调用R函数(即
sumc()
)。请执行以下操作:

// [[Rcpp::export]]
double mycall(){
  Environment base("package:base");
  Function sumc = base["sum"];
  Environment env = Environment::global_env();
  NumericVector Time = env["Time"];
  double res = sum(Time);
  return res;
}

这里
sum()
是Rcpp的sugar函数。

谢谢@Dirk Eddelbuettel,我尝试最后一种方法的原因是我想复制下面的R矢量化求和(Time[TimeYes,这里有几篇文章(包括昨天!!)介绍如何进行索引。感谢您接受答案,您也可以向上投票(单击向上箭头)。
#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;
}
trycpp.cpp:10:25: error: cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization
double res = sumc(Time);
// [[Rcpp::export]]
double mycall(){
  Environment base("package:base");
  Function sumc = base["sum"];
  Environment env = Environment::global_env();
  NumericVector Time = env["Time"];
  double res = sum(Time);
  return res;
}