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:如何将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; },r,rcpp,r-package,rcpp11,R,Rcpp,R Package,Rcpp11,但是,如果我想将以下调用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)

但是,如果我想将以下调用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 )
}
random=函数(A,T){
如果(!是矩阵(A)){
A=减少(“+”,A)/T
} 
#全局常数和阈值
n=nrow(A)
B_0=3
w=min(sqrt(n),sqrt(T*log(n)))
q=B_0*log(n)/(sqrt(n)*w)
A2=米阿巴尔(A)

diag(A2)那么你在问如何制作一个R包?有很多很好的教程

一级近似值:

  • 将您的文件复制到,例如文件
    R/random.R
  • 通过编写
    man/random.Rd
    或通过学习软件包
    roxygen2
    手动处理函数的帮助文件
  • 确保您知道
    名称空间
    的用途,并且
    说明
    是正确的
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 )
}