如何访问 R 中的帮助/文档 .rd 源文件?

发布于 2024-12-05 19:20:13 字数 363 浏览 0 评论 0原文

在 R 中,一个非常巧妙的功能是函数的源代码可以作为工作区中的对象进行访问。

因此,如果我想知道 grep() 的源代码,我可以简单地在控制台中输入 grep 并读取代码。

同样,我可以通过在控制台中输入 ?grep 来阅读 grep 的文档。

问题:如何获取函数文档的源代码?换句话说,我在哪里可以找到 .rd 文件?

我发现研究编写良好的代码的源代码是学习习惯用法的绝佳方法。现在我想研究如何为一些非常具体的案例编写文档。我无法在 R 安装中找到任何基本 R 函数的文档文件。也许我一直在寻找错误的地方。

In R, one very neat feature is that the source code of functions is accessible as objects in the workspace.

Thus, if I wanted to know the source code of, for example, grep() I can simply type grep into the console and read the code.

Similarly, I can read the documentation for grep by typing ?grep into the console.

Question: How can I get the source code for the documentation of a function? In other words, where do I find the .rd files?

I find studying the source of well-written code an excellent way of learning the idioms. Now I want to study how to write documentation for some very specific cases. I have not been able to find the documentation files for any of the base R functions in my R installation. Perhaps I have been looking in the wrong place.

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

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

发布评论

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

评论(1

樱桃奶球 2024-12-12 19:20:13

看来你可以从已安装的 R 中提取 Rd 源。我正在使用 R-devel (2011-09-05 r56942)。

获取基础包的 Rd 数据库。

library(tools)
db <- Rd_db("base")

在 Rd DB 的名称中搜索“grep.Rd”,例如:

grep("grep.Rd", names(db), value = TRUE)
[1] "d:/murdoch/recent/R64/src/library/base/man/agrep.Rd"
[2] "d:/murdoch/recent/R64/src/library/base/man/grep.Rd" 

仅获取 grep 的 Rd 对象。

db[grep("/grep.Rd", names(db))]

看来你可以从已安装的 R 中提取 Rd 源。我正在使用 R-devel (2011-09-05 r56942)。

获取基础包的 Rd 数据库。

library(tools)
db <- Rd_db("base")

在 Rd DB 的名称中搜索“grep.Rd”,例如:

grep("grep.Rd", names(db), value = TRUE)
[1] "d:/murdoch/recent/R64/src/library/base/man/agrep.Rd"
[2] "d:/murdoch/recent/R64/src/library/base/man/grep.Rd" 

仅获取 grep 的 Rd 对象。

d:/murdoch/recent/R64/src/library/base/man/grep.Rd` \title{Pattern Matching and Replacement} \name{grep} \alias{grep} \alias{grepl} \alias{sub} \alias{gsub} \alias{regexpr} \alias{gregexpr} \alias{regexec} \keyword{character} \keyword{utilities} \description{ \code{grep}, \code{grepl}, \code{regexpr} and \code{gregexpr} search for matches to argument \code{pattern} within each element of a character vector: they differ in the format of and amount of detail in the results. \code{sub} and \code{gsub} perform replacement of the first and all matches respectively. }\usage{ ... ...

有一些工具可用于从 Rd 对象获取组件,因此您可以将搜索细化为关键字或名称,请参阅 ?Rd_db 中的示例并尝试此操作。

lapply(db, tools:::.Rd_get_metadata, "name")

It seems you can extract the Rd sources from an installed R. I'm using R-devel (2011-09-05 r56942).

Get the database of Rd for the base package.

library(tools)
db <- Rd_db("base")

Search for "grep.Rd" in the names of the Rd DB, for example:

grep("grep.Rd", names(db), value = TRUE)
[1] "d:/murdoch/recent/R64/src/library/base/man/agrep.Rd"
[2] "d:/murdoch/recent/R64/src/library/base/man/grep.Rd" 

Get just the Rd object for grep.

db[grep("/grep.Rd", names(db))]

It seems you can extract the Rd sources from an installed R. I'm using R-devel (2011-09-05 r56942).

Get the database of Rd for the base package.

library(tools)
db <- Rd_db("base")

Search for "grep.Rd" in the names of the Rd DB, for example:

grep("grep.Rd", names(db), value = TRUE)
[1] "d:/murdoch/recent/R64/src/library/base/man/agrep.Rd"
[2] "d:/murdoch/recent/R64/src/library/base/man/grep.Rd" 

Get just the Rd object for grep.

d:/murdoch/recent/R64/src/library/base/man/grep.Rd` \title{Pattern Matching and Replacement} \name{grep} \alias{grep} \alias{grepl} \alias{sub} \alias{gsub} \alias{regexpr} \alias{gregexpr} \alias{regexec} \keyword{character} \keyword{utilities} \description{ \code{grep}, \code{grepl}, \code{regexpr} and \code{gregexpr} search for matches to argument \code{pattern} within each element of a character vector: they differ in the format of and amount of detail in the results. \code{sub} and \code{gsub} perform replacement of the first and all matches respectively. }\usage{ ... ...

There are tools for getting the components from the Rd objects, so you can refine searching to keywords or name, see examples in ?Rd_db and try this.

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