Rcpp 无法在初始化时将“SEXP {aka SEXPREC*}"转换为“double" [英] Rcpp cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization

查看:44
本文介绍了Rcpp 无法在初始化时将“SEXP {aka SEXPREC*}"转换为“double"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to duplicate the R vectorised sum in Rcpp

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

I first try the following trouble-free code:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
double call(NumericVector x){
  return sum(x);
}

输入call(Time)

> call(Time)
[1] 1919853

然后一个环境版本,也很好用,

Then an environment version, also works well,

#include <Rcpp.h>
using namespace Rcpp;

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

输入call()

> call()
[1] 1919853

现在我正在尝试一些奇怪的东西,

Now I am trying something weird as following,

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

这次我收到一条错误信息:

This time I got a error message:

trycpp.cpp:10:25: error: cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization
double res = sumc(Time);

知道出了什么问题吗?

推荐答案

您不能在 Rcpp 的向量之一上调用 R 函数(即 sumc().这样做:

You cannot call an R function (ie sumc() on one of Rcpp's vectors. Do this:

// [[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糖函数.

Here sum() is the Rcpp sugar function.

这篇关于Rcpp 无法在初始化时将“SEXP {aka SEXPREC*}"转换为“double"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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