2017-03-07 126 views
0

程序包名稱爲pareto。這是.c文件中src目錄:如何在R程序包中調用C函數

#include <R.h> 
#include <math.h> 
#include "Rinternals.h" 
#include "R_ext/Rdynload.h" 

static void dpareto(double *x, double *a, double *b, int *n, int *islog, 
          double *den){ 
    int length = n[0]; 
    int i; 
    int isLog = islog[0]; 
    for (i = 0; i < length; i++){ 
     if (a[i] > 0 && b[i] > 0) { 
      if (x[i] > a[i]) 
       den[i] = log(b[i]) + b[i] * log(a[i]) - (b[i] + 1) * log(x[i]); 
      else 
       den[i] = R_NegInf; 
      if (!isLog) 
       den[i] = exp(den[i]); 
     } 
     else { 
      den[i] = R_NaN; 
     } 
    } 
} 

static R_CMethodDef DotCEntries[] = { 
    {"dpareto", (DL_FUNC) dpareto, 6}, 
    {NULL} 
}; 

void R_init_pareto(DllInfo *info) 
{ 
    R_registerRoutines(info, DotCEntries, NULL, NULL, NULL); 
} 

R目錄,相應.R文件是:

#' @useDynLib pareto 
#' @export 
dpareto <- function(x, a, b, log = FALSE) { 
    nx <- length(x) 
    na <- length(a) 
    nb <- length(b) 
    n <- max(nx, na, nb) 
    if (nx < n) x <- rep(x, length.out = n) 
    if (na < n) a <- rep(a, length.out = n) 
    if (nb < n) b <- rep(b, length.out = n) 
    rt <- .C("dpareto", as.double(x), as.double(a), as.double(b), as.integer(n), 
      as.integer(log), den = double(n), PACKAGE="pareto") 
    rt$den 

} 

記錄了roxygen後,NAMESPACE有:

export(dpareto) 
useDynLib(pareto) 

但是這個軟件包不能通過檢查,並且保持基因不變速度錯誤消息:

"dpareto" not available for .C() for package "pareto" 
Calls: dpareto -> .C 

我真的不知道哪一步我犯了一個錯誤。

+0

'.c'函數是正確的,我成功地在一個包內調用。 –

回答

0

瑣碎的錯誤。只是在void R_init_pareto我用了一個錯誤的包名。如此愚蠢的錯誤。

1

您將static關鍵字添加到您的dpareto函數定義中。這意味着該功能不會被導出,所以R不會看到它。刪除static並重試。

+0

@洪Ooi不起作用。 –

+0

Bs He:確保共享庫已加載,即檢查文件'NAMESPACE'作爲'useDynLib(「pareto」)'。 –