这些都不显示pnorm函数的源代码。
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函数的源代码?
发布于 2012-12-26 11:07:33
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语言编写的,所以上一个问题view source code in R中的建议只是外围有用的(其中大部分集中在隐藏在名称空间中的函数上)。
Uwe Ligges的article in R news (p.43)是一个很好的通用参考。从该文档中:
在查看R源代码时,有时会出现对以下函数之一的调用:.C()、.Call()、.Fortran()、.External()或.Internal()
.Primitive()。这些函数调用编译代码中的入口点,如共享对象、静态库或动态链接库。因此,如果需要完全理解代码,则有必要查看编译代码的源代码。..。如果调用R函数是.Primitive()或.Internal(),第一步是在文件‘$R HOME/src/main/names.c’中查找入口点。对于实现‘simple’R函数()的代码,这是在下面的示例中完成的。
(之所以强调,是因为您询问的精确函数(sum)在Ligges的文章中有所介绍。)
根据您想要深入研究代码的严重程度,按照Ligges的建议下载并解压缩源代码可能是值得的(例如,您可以使用命令行工具,如grep来搜索源代码)。对于更多的非正式检查,您可以通过R Subversion server或Winston Chang's github mirror在线查看源代码(这里的链接专门指向src/nmath/pnorm.c)。(要想找到合适的位置,src/nmath/pnorm.c需要对R源代码的结构有一定的了解。)
mean和sum都是用summary.c实现的。
发布于 2015-04-17 18:49:09
我知道这篇文章已经有两年多的历史了,但我想这篇文章可能对浏览这个问题的一些用户有用。
基本上,我只是将我的答案复制到this other similar question上,这样对于一些想要探索C源文件的R用户来说,它可能会很有用。
show_c_source函数,该函数将在GitHub上搜索C源文件中的相关代码片段。适用于.Internal和.Primitive函数。pryr::show_c_source(.Internal(match.call(definition,(match.call)# .Internal(match.call(definition,call,expand.dots)) body调用,expand.dots))
这将把您带到this page,其中显示了unique.c包含了函数do_matchcall.
names.c文件的基础上,并使用find-in-files来确定源代码的位置。有些函数有特定于平台的文件,还有一些函数有多个包含相关源代码的文件。但对于其余部分,映射已经建立得很好了,至少对于当前版本(3.1.2)是这样。发布于 2012-12-26 11:34:31
> methods(mean)
[1] mean.data.frame mean.Date mean.default mean.difftime mean.IDate*
[6] mean.POSIXct mean.POSIXlt mean.yearmon* mean.yearqtr*
Non-visible functions are asterisked
> mean.default
function (x, trim = 0, na.rm = FALSE, ...)
{
if (!is.numeric(x) && !is.complex(x) && !is.logical(x)) {
warning("argument is not numeric or logical: returning NA")
return(NA_real_)
}
if (na.rm)
x <- x[!is.na(x)]
if (!is.numeric(trim) || length(trim) != 1L)
stop("'trim' must be numeric of length one")
n <- length(x)
if (trim > 0 && n) {
if (is.complex(x))
stop("trimmed means are not defined for complex data")
if (any(is.na(x)))
return(NA_real_)
if (trim >= 0.5)
return(stats::median(x, na.rm = FALSE))
lo <- floor(n * trim) + 1
hi <- n + 1 - lo
x <- sort.int(x, partial = unique(c(lo, hi)))[lo:hi]
}
.Internal(mean(x))
}
<bytecode: 0x155ef58>
<environment: namespace:base>https://stackoverflow.com/questions/14035506
复制相似问题