程序包名稱爲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
我真的不知道哪一步我犯了一個錯誤。
'.c'函數是正確的,我成功地在一個包內調用。 –