gpt4 book ai didi

r - C++ 函数不可用

转载 作者:行者123 更新时间:2023-12-04 13:37:36 24 4
gpt4 key购买 nike

我有以下文件 cumsum_bounded.cpp

#include <Rcpp.h>
using namespace Rcpp;

//' Cumulative sum.
//' @param x numeric vector
//' @param low lower bound
//' @param high upper bound
//' @param res bounded numeric vector
//' @export
//' @return bounded numeric vector
// [[Rcpp::export]]
NumericVector cumsum_bounded(NumericVector x, double low, double high) {
NumericVector res(x.size());
double acc = 0;
for (int i=0; i < x.size(); ++i) {
acc += x[i];
if (acc < low) acc = low;
else if (acc > high) acc = high;
res[i] = acc;
}
return res;
}

然后我构建并重新加载并测试我的新功能。
cumsum_bounded(c(1, -2, 3), low = 2, high = 10)
[1] 1 0 3

然后我构建文档。 devtools::document()
当我 Build & Reload一切都编译得很好。

但是当我运行 cumsum_bounded(c(1, 2, 3), low= 2, high = 10)我收到错误:
Error in .Call("joshr_cumsum_bounded", PACKAGE = "joshr", x, low, high) : 
"joshr_cumsum_bounded" not available for .Call() for package "joshr"

命名空间
# Generated by roxygen2: do not edit by hand

export(cumsum_bounded)

更新:

如果我像上面那样创建一个新项目并且不要使用 Build & Reload函数而是 devtools::loadall(),它会起作用。但是一旦我按下 Build & Reload按钮,它会横向移动。

最佳答案

你可能需要这条线

useDynLib(<pkg>) ## substitute your package name for <pkg>

在您的 NAMESPACE 文件中。如果您使用的是 roxygen2,则可以添加一行,例如 #' @useDynLib <pkg>在您的文档中的某处,用您的包名替换 <pkg>作为适当的。

编辑:为了响应您的其他错误消息,您可能需要从 Rcpp 导入某些内容,例如添加行 @importFrom Rcpp evalCpp .

关于r - C++ 函数不可用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36605955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com