如何查看 R .Internal 或 .Primitive 函数的源代码? [英] How to see the source code of R .Internal or .Primitive function?

查看:74
本文介绍了如何查看 R .Internal 或 .Primitive 函数的源代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这些都没有显示pnorm函数的源代码,

Neither of these show the source code of pnorm function,

stats:::pnorm
getAnywhere(pnorm)  

如何查看pnorm的源代码?

sum
 (..., na.rm = FALSE)  .Primitive("sum")
.Primitive("sum")
function (..., na.rm = FALSE)  .Primitive("sum")
methods(sum)
no methods were found

还有,如何查看sum 函数的源代码?

and, how can I see source code of the sum function?

推荐答案

pnorm的R源代码是:

function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE) 
.Call(C_pnorm, q, mean, sd, lower.tail, log.p)

所以,从技术上讲,输入pnorm"确实会显示源代码.然而,更有用的是:pnorm 的核心是用 C 编码的,所以上一个问题中的建议 查看 R 中的源代码 只是外围有用(大部分集中在隐藏在命名空间等中的函数).

So, technically speaking, typing "pnorm" does show you the source code. However, more usefully: The guts of pnorm are coded in C, so the advice in the previous question view source code in R is only peripherally useful (most of it concentrates on functions hidden in namespaces etc.).

Uwe Ligges 的 R 新闻中的文章(第 43 页) 是一个很好的一般参考.来自该文件:

Uwe Ligges's article in R news (p. 43) is a good general reference. From that document:

查看R源代码时,有时会调用显示以下函数之一:.C(),.Call()、.Fortran()、.External() 或 .Internal()和 .Primitive().这些函数调用编译代码中的入口点,例如共享对象,静态库或动态链接库.所以,如果完全理解代码,则有必要查看编译代码的来源必需的....第一步是查找文件‘$R HOME/src/main/names.c’中的入口点,如果调用 R 函数是 .Primitive() 或.内部的().这是在以下示例中为实现简单"R 函数的代码完成的sum().

When looking at R source code, sometimes calls to one of the following functions show up: .C(), .Call(), .Fortran(), .External(), or .Internal() and .Primitive(). These functions are calling entry points in compiled code such as shared objects, static libraries or dynamic link libraries. Therefore, it is necessary to look into the sources of the compiled code, if complete understanding of the code is required. ... The first step is to look up the entry point in file ‘$R HOME/src/main/names.c’, if the calling R function is either .Primitive() or .Internal(). This is done in the following example for the code implementing the ‘simple’ R function sum().

(强调是因为您询问的精确函数 (sum) 已在 Ligges 的文章中介绍.)

(Emphasis added because the precise function you asked about (sum) is covered in Ligges's article.)

根据您想深入研究代码的认真程度,它可能值得下载和按照 Ligges 的建议解压源代码(例如,您可以使用命令行工具例如 grep 来搜索源代码).更随意的检查,可以查看通过 R Subversion 服务器Winston Chang 的 github 镜像(这里的链接专门针对 <代码>src/nmath/pnorm.c).(猜测正确的位置,src/nmath/pnorm.c,需要熟悉 R 源代码的结构.)

Depending on how seriously you want to dig into the code, it may be worth downloading and unpacking the source code as Ligges suggests (for example, then you can use command-line tools such as grep to search through the source code). For more casual inspection, you can view the sources online via the R Subversion server or Winston Chang's github mirror (links here are specifically to src/nmath/pnorm.c). (Guessing the right place to look, src/nmath/pnorm.c, takes some familiarity with the structure of the R source code.)

meansum 都在 summary.c.

这篇关于如何查看 R .Internal 或 .Primitive 函数的源代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆