查看 R 包的源代码

发布于 2024-08-12 15:35:46 字数 45 浏览 8 评论 0原文

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

Is there an easy way to view the source of an R package (or a method in a package), from within the interactive environment?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月亮坠入山谷 2024-08-19 15:35:47

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

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

另请参阅R 新闻第 6/4 卷,2006 年 10 月

Just enter the name of a function/method without parentheses:

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

See also R-Help Desk - Accessing the Sources in R News Volume 6/4, October 2006.

夏雨凉 2024-08-19 15:35:47

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

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

How you find the source code depends on the type of function. See my answer to this related question.

As rcs pointed out, if you want to specify a package, you can use ::.

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

Not all functions from a package will be exported (i.e. made publically available); for these you need to use :::.

> 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.
眼泪都笑了 2024-08-19 15:35:47

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

有时,仅 print(funcOfInterest.class) 是不够的。然后尝试 print(getAnywhere(funcOfInterest.class)) 。

To find out which methods you want to see, write methods(funcOfInterest)

Sometimes it does not suffice to print(funcOfInterest.class). Try print(getAnywhere(funcOfInterest.class)) then.

幻梦 2024-08-19 15:35:47

https://cloud.r-project.org/src/contrib 并用您最喜欢的编辑器打开它。找到函数定义(您可以使用 grep 来实现)。有时您也可以找到有用的介绍。

Download package source from https://cloud.r-project.org/src/contrib and open it with your favorite editor. Find the function definition (you can use grep for that). Sometimes you can find a useful introduction as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文