在交互环境中,有没有一种简单的方法可以查看R包(或包中的方法)的源代码?
只需输入函数/方法的名称,不需要括号:
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。
根据函数的类型,你如何找到源代码是不同的。请参见我的回答来解决相关问题。
正如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.
要查看您想要查看的方法,请编写methods(funcOfInterest)
有时,print(funcOfInterest.class)
并不足够。然后尝试print(getAnywhere(funcOfInterest.class))
。
grep
)。有时您还可以找到有用的介绍。