Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
165 views
in Technique[技术] by (71.8m points)

How to access the help/documentation .rd source files in 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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))]
$`d:/murdoch/recent/R64/src/library/base/man/grep.Rd`
itle{Pattern Matching and Replacement}

ame{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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...