查看 R 包的源代码

25

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

4个回答

20

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

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

请参见 R News Volume 6/4, October 2006 中的R-Help Desk - Accessing the Sources


15

根据函数的类型,你如何找到源代码是不同的。请参见我的回答来解决相关问题。

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

> lattice::xyplot
function (x, data, ...) 
UseMethod("xyplot")
<environment: namespace:lattice>

并非所有包中的函数都被导出(即公开可用);对于这些函数,您需要使用:::

> 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.

9

要查看您想要查看的方法,请编写methods(funcOfInterest)

有时,print(funcOfInterest.class)并不足够。然后尝试print(getAnywhere(funcOfInterest.class))


1
请注意,如果您正在交互式地工作,则不需要显式调用“print”。 - Dason

3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接