Ben Bolkers对this question和article by Uwe Ligges的回答已经非常有用了
当我尝试“解码”原始或内部R函数时。
但原始R函数如何与其相应的C函数相连?
我想不知怎的.Primitive
必须提供这个缺失的链接。
以is.na
:
> is.na
function (x) .Primitive("is.na")
文件“names.c”中的 FUNTAB R_FunTab[]
包含
{"is.na", do_isna, 0, 1, 1, {PP_FUNCALL, PREC_FN, 0}},
表示is.na
使用C函数do_isna
。
do_isna
在文件“coerce.c”中定义:
SEXP attribute_hidden do_isna(SEXP call, SEXP op, SEXP args, SEXP rho)
{
SEXP ans, dims, names, x;
R_xlen_t i, n;
checkArity(op, args);
check1arg(args, call, "x");
if (DispatchOrEval(call, op, "is.na", args, rho, &ans, 1, 1))
return(ans);
PROTECT(args = ans);
#ifdef stringent_is
if (!isList(CAR(args)) && !isVector(CAR(args)))
errorcall_return(call, "is.na " R_MSG_list_vec);
#endif
x = CAR(args);
n = xlength(x);
PROTECT(ans = allocVector(LGLSXP, n));
if (isVector(x)) {
PROTECT(dims = getAttrib(x, R_DimSymbol));
if (isArray(x))
PROTECT(names = getAttrib(x, R_DimNamesSymbol));
else
PROTECT(names = getAttrib(x, R_NamesSymbol));
}
else dims = names = R_NilValue;
switch (TYPEOF(x)) {
case LGLSXP:
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = (LOGICAL(x)[i] == NA_LOGICAL);
break;
case INTSXP:
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = (INTEGER(x)[i] == NA_INTEGER);
break;
case REALSXP:
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = ISNAN(REAL(x)[i]);
break;
case CPLXSXP:
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = (ISNAN(COMPLEX(x)[i].r) ||
ISNAN(COMPLEX(x)[i].i));
break;
case STRSXP:
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = (STRING_ELT(x, i) == NA_STRING);
break;
/* Same code for LISTSXP and VECSXP : */
#define LIST_VEC_NA(s) \
if (!isVector(s) || length(s) != 1) \
LOGICAL(ans)[i] = 0; \
else { \
switch (TYPEOF(s)) { \
case LGLSXP: \
case INTSXP: \
LOGICAL(ans)[i] = (INTEGER(s)[0] == NA_INTEGER); \
break; \
case REALSXP: \
LOGICAL(ans)[i] = ISNAN(REAL(s)[0]); \
break; \
case STRSXP: \
LOGICAL(ans)[i] = (STRING_ELT(s, 0) == NA_STRING); \
break; \
case CPLXSXP: \
LOGICAL(ans)[i] = (ISNAN(COMPLEX(s)[0].r) \
|| ISNAN(COMPLEX(s)[0].i)); \
break; \
default: \
LOGICAL(ans)[i] = 0; \
} \
}
case LISTSXP:
for (i = 0; i < n; i++) {
LIST_VEC_NA(CAR(x));
x = CDR(x);
}
break;
case VECSXP:
for (i = 0; i < n; i++) {
SEXP s = VECTOR_ELT(x, i);
LIST_VEC_NA(s);
}
break;
case RAWSXP:
/* no such thing as a raw NA */
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = 0;
break;
default:
warningcall(call, _("%s() applied to non-(list or vector) of type '%s'"),
"is.na", type2char(TYPEOF(x)));
for (i = 0; i < n; i++)
LOGICAL(ans)[i] = 0;
}
if (dims != R_NilValue)
setAttrib(ans, R_DimSymbol, dims);
if (names != R_NilValue) {
if (isArray(x))
setAttrib(ans, R_DimNamesSymbol, names);
else
setAttrib(ans, R_NamesSymbol, names);
}
if (isVector(x))
UNPROTECT(2);
UNPROTECT(1);
UNPROTECT(1); /*ans*/
return ans;
}
但是,如果我们想要评估is.na(x=3)
,那么参数是怎样的
生成了call
,op
,args
,rho
?
至少必须使用一些外部信息,x=3
是不够的。
此外,乍一看根本没有使用x=3
,当然这肯定是错误的:
> is.na
function (x) .Primitive("is.na")
.Primitive
的R代码没有给出提示:
> .Primitive
function (name) .Primitive(".Primitive")
考虑到所有这些因素,isNA
的{{1}}明显优秀副本失败就不足为奇了:
is.na
直截了当:
所有C函数> isNA <- function (x) .Primitive("is.na")
> isNA
function (x) .Primitive("is.na")
> is.na
function (x) .Primitive("is.na")
> isNA(x=3)
function (x) .Primitive("is.na")
> is.na(x=3)
[1] FALSE
都有这些参数
do_...
,call
,op
,args
。
当调用原始R函数时,它们通过什么公式计算?
答案 0 :(得分:7)
好问题。我在gdb R -d gdb
下启动了R,在do_isna
设置了断点,然后继续R并输入is.na(3)
。
$ R -d gdb
(gdb) run
Starting program: /home/mtmorgan/bin/R-3-3-branch/bin/exec/R --no-save --no-restore --silent
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
> ## break, cntrl-C
Program received signal SIGINT, Interrupt.
0x00007ffff722fd83 in __select_nocancel () at ../sysdeps/unix/syscall-template.S:81
81 ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) b do_isna
Breakpoint 1 at 0x7ffff77e0b3b: file /home/mtmorgan/src/R-3-3-branch/src/main/coerce.c, line 1982.
(gdb) continue
Continuing.
> is.na(3)
Breakpoint 1, do_isna (call=0x1838888, op=0x628218, args=0x1838770, rho=0x63f648)
at /home/mtmorgan/src/R-3-3-branch/src/main/coerce.c:1982
1982 checkArity(op, args);
(gdb)
在gdb提示符下我问了
(gdb) where
#0 do_isna (call=0x1838888, op=0x628218, args=0x1838770, rho=0x63f648) at /home/mtmorgan/src/R-3-3-branch/src/main/coerce.c:1982
#1 0x00007ffff7869170 in Rf_eval (e=0x1838888, rho=0x63f648) at /home/mtmorgan/src/R-3-3-branch/src/main/eval.c:717
#2 0x00007ffff78b36af in Rf_ReplIteration (rho=0x63f648, savestack=0, browselevel=0, state=0x7fffffffcaf0) at /home/mtmorgan/src/R-3-3-branch/src/main/main.c:258
...
从#2开始,Rf_ReplIteration是试图评估is.na(3)
的REPL(读取 - 评估 - 打印循环)。它提供了调用函数的环境。当它在第258行调用Rf_eval()
时,它知道环境和呼叫
(gdb) call Rf_PrintValue(rho)
<environment: R_GlobalEnv>
(gdb) call Rf_PrintValue(thisExpr)
is.na(3)
通过#1(eval.c:717),R已计算出op
和tmp
的值。
(gdb) call Rf_PrintValue(op)
function (x) .Primitive("is.na")
(gdb) call TYPEOF(op)
$2 = 8
(类型8是'BUILTINSXP',来自Rinternals.h中的表)。它通过发现e
是LANGSXP(第614行),即。是一个SYMSXP(第670行),并且它引用的函数(op
)是BUILTINSXP(行700)。然后使用(第717行)
(gdb) call PRIMFUN(op)
$8 = (SEXP (*)(SEXP, SEXP, SEXP, SEXP)) 0x7ffff77e0b20 <do_isna>
发现它应该使用它发现的值来调用do_isna
。
希望这能消除一些神秘感,并指向代码的相关部分。