How to access the help/documentation .rd source files in R?
Asked Answered
T

1

20

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.

Trumaine answered 21/9, 2011 at 6:53 Comment(5)
there is a stack site specially dedicated to stats and analysis stats.stackexchange.comBitten
I have never seen this done, but it would be nice if there was a way to extract Rd files from rda files. If you want to have .Rd files, you can always download a package (or in the case of base package, R source).Revenue
@Bitten Yes, I am aware of that, thank you. But this is a programming question, not a statistical analysis question, so would be off-topic on CrossValidated.Trumaine
Do you mean you want code to give you the Rd sources from an installed build of R? I just go for the source outside of R, e.g. [R sources]\src\library\base\man\grep.RdBateau
@Bateau Code to give me this would be first prize. But thank you also for the link to the correct folder to find the docs files. (Clearly my OS file searching skills need some polishing!)Trumaine
B
37

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`
\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")
Bateau answered 21/9, 2011 at 7:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.