查看R包的来源

时间:2009-11-24 08:58:45

标签: r r-faq

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

4 个答案:

答案 0 :(得分:18)

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

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 - 访问来源

答案 1 :(得分:15)

如何找到源代码取决于函数的类型。请参阅my answer此相关问题。

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

答案 2 :(得分:9)

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

有时它不足以print(funcOfInterest.class)。然后尝试print(getAnywhere(funcOfInterest.class))

答案 3 :(得分:1)

https://cloud.r-project.org/src/contrib下载程序包源,然后使用您喜欢的编辑器将其打开。找到函数定义(您可以使用grep)。有时您也可以找到有用的介绍。