【发布时间】:2018-08-09 21:40:19
【问题描述】:
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"]),这样我就可以遍历零。
错误:“Cannot convert 'SEXP' to 'Rcpp::traits::storage_type::type {aka double}' in assignment”出现在上面 for 循环的最后一行。
有没有办法将 res 从 SEXP 转换为 float 或 double?
我意识到这里有人问过类似的问题: Rcpp cannot convert ‘SEXP {aka SEXPREC*}’ to ‘double’ in initialization 但这个问题是通过使用 Rcpp 糖函数而不是 R 函数来解决的,以避免将 SEXP 转换为双精度。
如果没有办法从 SEXP 转换为浮点数或双精度数,除了在 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);
}
获取此代码后,将出现上面指定的错误。
【问题讨论】:
-
欢迎来到 StackOverflow!您的问题中有很多废话,但没有minimal reproducible example,因此我们只能说很少。请编辑以提供触发错误的最小但完整的内容。
-
你试过
as<>操作符吗? @DirkEddelbuettel 撰写了一篇对这类事情非常透彻的文章。可以找到here。 -
Psst @JosephWood Dirk 撰写了许多精彩的 Rcpp 文章;但是,我写了一个:)
-
@coatless,我觉得自己像个白痴。那是超级粗心和无意的。我每天都在Rcpp Galary,发现那篇文章很快,复制链接,甚至懒得看作者。我非常抱歉。我非常尊重您,并经常宣传您的工作和网站。
-
as 运算符成功了。我之前曾尝试过,但显然语法不正确。谢谢!
标签: r floating-point type-conversion double rcpp