r - Rcpp:如何将R函数和Rcpp函数组合在一起制作一个包
问题描述
假设我在名为 test.cpp 的文件中有以下 c++ 代码
#include <Rcpp.h>
//[[Rcpp::export]]
Rcpp::NumericMatrix MyAbar (const Rcpp::NumericMatrix & x, int T){
unsigned int outrows = x.nrow(), i = 0, j = 0;
double d;
Rcpp::NumericMatrix out(outrows,outrows);
// Rcpp::LogicalVector comp;
for (i = 0; i < outrows - 1; i++){
Rcpp::NumericVector v1 = x.row(i);
Rcpp::NumericVector ans(outrows);
for (j = i + 1; j < outrows ; j ++){
d = mean(Rcpp::runif( T ) < x(i,j));
out(j,i)=d;
out(i,j)=d;
}
}
return out;
}
我知道通过以下命令,我可以拥有自己的包
Rcpp.package.skeleton("test",cpp_files = "~/Desktop/test.cpp")
但是,如果我想将以下调用 Rcpp 函数的 R 函数组合到包中怎么办
random = function(A, T){
if (!is.matrix(A)){
A = Reduce("+",A)/T
}
# global constant and threshold
n = nrow(A)
B_0 = 3
w = min(sqrt(n),sqrt(T * log(n)))
q = B_0 * log(n) / (sqrt(n) * w)
A2 = MyAbar(A)
diag(A2) <- NA
K = A2 <= rowQuantiles(A2, probs=q, na.rm =TRUE)
diag(K) = FALSE
P = K %*% A * ( 1/(rowSums(K) + 1e-10))
return( (P + t(P))*0.5 )
}
我怎样才能做到?
解决方案
所以你问如何制作一个 R 包?有很多很好的教程。
初步近似:
- 将您的文件复制到文件中
R/random.R
man/random.Rd
通过编写或学习包手动处理函数的帮助文件roxygen2
- 确保你知道什么
NAMESPACE
是DESCRIPTION
正确的
推荐阅读
- javascript - 如何检查嵌套数组是否为空?
- android - 从 Firebase 问题中删除 android 应用
- reactjs - ReactJS:在上传之前调整图像大小
- vba - VBA wordapp.document.open 和 selection.WholeStory
- c# - JSON.NET 无法反序列化 ulong 标志类型枚举
- python - 如果我不清除缓存,为什么我的 Flask 应用程序不呈现 CSS?
- php - PHP POST api 在邮递员中工作,但在颤振应用程序中不工作
- c - 不相交路径 - 查找矩阵中不相交路径的最大数量
- r - 如何知道 R 中某个命名颜色的 rgb 颜色代码?
- python - 将 sympy 函数转换为 lambda 函数