查看R包的来源

puf*_*ish 24 r r-faq

有没有一种简单的方法可以在交互式环境中查看R包的源代码(或包中的方法)?

rcs*_*rcs 18

只需输入不带括号的函数/方法的名称:

R> base::rev.default 
function (x) 
if (length(x)) x[length(x):1L] else x
<environment: namespace:base>
Run Code Online (Sandbox Code Playgroud)

另请参见R-服务台-访问来源[R新闻6/4卷,2006年10月.


Ric*_*ton 15

如何找到源代码取决于函数的类型.请参阅对此相关问题的回答.

正如rcs所指出的,如果你想指定一个包,你可以使用::.

> lattice::xyplot
function (x, data, ...) 
UseMethod("xyplot")
<environment: namespace:lattice>
Run Code Online (Sandbox Code Playgroud)

并非所有包中的功能都将被导出(即公开发布); 对于这些你需要使用:::.

> lattice::xyplot.formula
Error: 'xyplot.formula' is not an exported object from 'namespace:lattice'

> lattice:::xyplot.formula
function (x, data = NULL, allow.multiple = is.null(groups) || 
    outer, outer = !is.null(groups), auto.key = FALSE, aspect = "fill", 
    panel = lattice.getOption("panel.xyplot"), prepanel = NULL, 
    scales = list(), strip = TRUE, groups = NULL, xlab, xlim, 
    ylab, ylim, drop.unused.levels = lattice.getOption("drop.unused.levels"), 
    ..., lattice.options = NULL, default.scales = list(), subscripts = !is.null(groups), 
    subset = TRUE) 
{
    formula <- x
    dots <- list(...)
# etc.
Run Code Online (Sandbox Code Playgroud)


Kar*_* W. 9

要找出您想要查看的方法,请写信 methods(funcOfInterest)

有时它还不够print(funcOfInterest.class).print(getAnywhere(funcOfInterest.class))然后试试.