Rcpp:将SEXP转换为float / double

问题描述 投票:1回答:1
SEXP callFunction1(List network, List words, Function testWordContinuity){
  SEXP res = testWordContinuity(network, words);
  return res;
}

for(int i=0; i<(n_epochs); i++){
  NumericVector outputMatchTracker = history["output.match.tracker"];
  outputMatchTracker[i] = callFunction1(network, words, testWordContinuity);
}

R中的testWordContinuity函数调用R中的另一个返回单个数字变量的函数

我用res做的就是使用for循环替换向量中的值。 for循环开始后的第一行是将outputMatchTracker分配给零向量(history [“output.match.tracker”]),这样我就可以遍历零。

错误:“无法将'SEXP'转换为'Rcpp :: traits :: storage_type <14> :: type {aka double}'赋值”发生在上面for循环的最后一行。

有没有办法将RES从SEXP转换为浮动或加倍?

我意识到这里有一个类似的问题:Rcpp cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization,但是这个问题是通过使用Rcpp糖函数而不是R函数来解决的,以避免将SEXP转换为double。

如果没有办法从SEXP转换为float或double,除了在Rcpp中编写R函数之外,还有一种解决这个问题的常用方法吗?

很高兴在必要时提供更多信息,

谢谢。

编辑:

最小可重复示例:

在Rcpp:

// [[Rcpp::export]]
SEXP callFunction(Function func){
  SEXP res = func();
  return(res);
}

// [[Rcpp::export]]
NumericVector func1(Function func){
  for(int i=0; i<10; i++){
    NumericVector vect(10);
    vect[i] = callFunction(func);
  }
  return(vect);
}

在获取此代码时,将显示上面指定的错误。

r floating-point type-conversion double rcpp
1个回答
0
投票

我相信你应该使用REAL宏,它返回一个指向double数组开头的指针。有关示例,请参阅https://cran.r-project.org/doc/manuals/r-release/R-exts.html#Attributes

© www.soinside.com 2019 - 2024. All rights reserved.