How to Access the Help/Documentation .Rd Source Files in R

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

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

How to get the corresponding Rd documentation file of a function

tools::Rd_db creates a named list of parsed .Rd files for an installed package. For instance, to print the contents of lubridate's .Rd file ymd.Rd, you could use:

## create list of parsed .Rd files
db <- tools::Rd_db("lubridate")

## names of .Rd files
names(db)
#> [1] "DateCoercion.Rd" "DateTimeUpdate.Rd" "Deprecated.Rd"
#> [4] "Duration-class.Rd" "Interval-class.Rd" "Period-class.Rd"
#> [7] "Timespan-class.Rd" "am.Rd" "as.duration.Rd"
#> [10] "as.interval.Rd" "as.period.Rd" "as_date.Rd"
#> [13] "date.Rd" "date_decimal.Rd" "day.Rd"
#> [16] "days_in_month.Rd" "decimal_date.Rd" "dst.Rd"
#> [19] "duration.Rd" "fit_to_timeline.Rd" "force_tz.Rd"
#> [22] "guess_formats.Rd" "hidden_aliases.Rd" "hms.Rd"
#> [25] "hour.Rd" "interval.Rd" "is.Date.Rd"
#> [28] "is.POSIXt.Rd" "is.difftime.Rd" "is.instant.Rd"
#> [31] "is.timespan.Rd" "lakers.Rd" "leap_year.Rd"
#> [34] "local_time.Rd" "lubridate-package.Rd" "make_datetime.Rd"
#> [37] "make_difftime.Rd" "minute.Rd" "month.Rd"
#> [40] "mplus.Rd" "now.Rd" "origin.Rd"
#> [43] "parse_date_time.Rd" "period.Rd" "period_to_seconds.Rd"
#> [46] "pretty_dates.Rd" "quarter.Rd" "reclass_date.Rd"
#> [49] "reclass_timespan.Rd" "rollback.Rd" "round_date.Rd"
#> [52] "second.Rd" "stamp.Rd" "time_length.Rd"
#> [55] "timespan.Rd" "today.Rd" "tz.Rd"
#> [58] "week.Rd" "with_tz.Rd" "within-interval.Rd"
#> [61] "year.Rd" "ymd.Rd" "ymd_hms.Rd"

## print ymd.Rd
db[["ymd.Rd"]]
#> \title{Parse dates with \strong{y}ear, \strong{m}onth, and \strong{d}ay components}\name{ymd}\alias{ymd}\alias{ydm}\alias{mdy}\alias{myd}\alias{dmy}\alias{dym}\alias{yq}\keyword{chron}\description{
#> Transforms dates stored in character and numeric vectors to Date or POSIXct
#> objects (see \code{tz} argument). These functions recognize arbitrary
#> non-digit separators as well as no separator. As long as the order of
#> formats is correct, these functions will parse dates correctly even when the
#> input vectors contain differently formatted dates. See examples.
#> }\usage{
#> ymd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> ydm(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> mdy(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> myd(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> dmy(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> dym(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"),
#> truncated = 0)
#>
#> yq(..., quiet = FALSE, tz = NULL, locale = Sys.getlocale("LC_TIME"))
#> }
#> ... ETCETERA ...

get the documentation of an R function from the help as a string

You can extract help file as text:

helptext <- help(truehist, package=MASS)
tools:::Rd2txt(utils:::.getHelpFile(as.character(helptext)))

After you get the file you can easily find the arguments part, search for "data:" and retrieve the following text.

Access elements from R's Rd file?

This document by Duncan Murdoch on parsing Rd files will be helpful, as will this SO post.

From these, you could probably try something like the following:

getauthors <- function(package){
db <- tools::Rd_db(package)
authors <- lapply(db,function(x) {
tags <- tools:::RdTags(x)
if("\\author" %in% tags){
# return a crazy list of results
#out <- x[which(tmp=="\\author")]
# return something a little cleaner
out <- paste(unlist(x[which(tags=="\\author")]),collapse="")
}
else
out <- NULL
invisible(out)
})
gsub("\n","",unlist(authors)) # further cleanup
}

We can then run this on a package or two:

> getauthors("knitr")
d:/RCompile/CRANpkg/local/3.0/knitr/man/eclipse_theme.Rd
" Ramnath Vaidyanathan"
d:/RCompile/CRANpkg/local/3.0/knitr/man/image_uri.Rd
" Wush Wu and Yihui Xie"
d:/RCompile/CRANpkg/local/3.0/knitr/man/imgur_upload.Rd
" Yihui Xie, adapted from the imguR package by Aaron Statham"
d:/RCompile/CRANpkg/local/3.0/knitr/man/knit2pdf.Rd
" Ramnath Vaidyanathan, Alex Zvoleff and Yihui Xie"
d:/RCompile/CRANpkg/local/3.0/knitr/man/knit2wp.Rd
" William K. Morris and Yihui Xie"
d:/RCompile/CRANpkg/local/3.0/knitr/man/knit_theme.Rd
" Ramnath Vaidyanathan and Yihui Xie"
d:/RCompile/CRANpkg/local/3.0/knitr/man/knitr-package.Rd
" Yihui Xie <http://yihui.name>"
d:/RCompile/CRANpkg/local/3.0/knitr/man/read_chunk.Rd
" Yihui Xie; the idea of the second approach came from Peter Ruckdeschel (author of the SweaveListingUtils package)"
d:/RCompile/CRANpkg/local/3.0/knitr/man/read_rforge.Rd
" Yihui Xie and Peter Ruckdeschel"
d:/RCompile/CRANpkg/local/3.0/knitr/man/rst2pdf.Rd
" Alex Zvoleff and Yihui Xie"
d:/RCompile/CRANpkg/local/3.0/knitr/man/spin.Rd
" Yihui Xie, with the original idea from Richard FitzJohn (who named it as sowsear() which meant to make a silk purse out of a sow's ear)"

And maybe tools:

> getauthors("tools")
D:/murdoch/recent/R64-3.0/src/library/tools/man/bibstyle.Rd
" Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/checkPoFiles.Rd
" Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/checkRd.Rd
" Duncan Murdoch, Brian Ripley"
D:/murdoch/recent/R64-3.0/src/library/tools/man/getDepList.Rd
" Jeff Gentry "
D:/murdoch/recent/R64-3.0/src/library/tools/man/HTMLlinks.Rd
"Duncan Murdoch, Brian Ripley"
D:/murdoch/recent/R64-3.0/src/library/tools/man/installFoundDepends.Rd
"Jeff Gentry"
D:/murdoch/recent/R64-3.0/src/library/tools/man/makeLazyLoading.Rd
"Luke Tierney and Brian Ripley"
D:/murdoch/recent/R64-3.0/src/library/tools/man/parse_Rd.Rd
" Duncan Murdoch "
D:/murdoch/recent/R64-3.0/src/library/tools/man/parseLatex.Rd
"Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/Rd2HTML.Rd
" Duncan Murdoch, Brian Ripley"
D:/murdoch/recent/R64-3.0/src/library/tools/man/Rd2txt_options.Rd
"Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/RdTextFilter.Rd
" Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/SweaveTeXFilter.Rd
"Duncan Murdoch"
D:/murdoch/recent/R64-3.0/src/library/tools/man/texi2dvi.Rd
" Originally Achim Zeileis but largely rewritten by R-core."
D:/murdoch/recent/R64-3.0/src/library/tools/man/tools-package.Rd
" Kurt Hornik and Friedrich Leisch Maintainer: R Core Team R-core@r-project.org"
D:/murdoch/recent/R64-3.0/src/library/tools/man/vignetteDepends.Rd
" Jeff Gentry "
D:/murdoch/recent/R64-3.0/src/library/tools/man/vignetteEngine.Rd
"Duncan Murdoch and Henrik Bengtsson."
D:/murdoch/recent/R64-3.0/src/library/tools/man/writePACKAGES.Rd
" Uwe Ligges and R-core."

Some functions have no author field, so this just drops those when it calls unlist at the end of getauthors, but the code could be modified slightly to return NULL values for those.

Also, further parsing is going to become a little bit difficult because package authors seem to use this field in very different ways. There's only one author field in devtools. There are a bunch in car, each of which contains an email address. Etc, etc. But this gets you to the available info, which you should be able to work with further.

Note: My previous version of this answer provided a solution if you have the full path of an Rd file, but didn't work if you were trying to do this for an installed package. Following Tyler's advice, I've worked out a more complete solution.

R: source() and path to source files

If you are distributing a script to colleagues, you should really not be writing a script that sources other scripts. What if you want to rename or move functions.R in the future? What if you need to modify a function in functions.R, but wrapper.R relies on the older version of that function? It's a flimsy solution that will cause headache. I would recommend either of the following instead.

  1. Put everything needed into a single, self-contained script and distribute that.

  2. If you really want to separate code into different files, write a package. Might sound like overkill, but packages can actually be very simple and lightweight. In the simplest form a package is just a directory with a DESCRIPTION and NAMESPACE file along with an R/ directory. Hadley breaks this down nicely: https://r-pkgs.org/whole-game.html.

Show an Rd file in viewer from Rstudio

I don't quite understand why you want to do this, but it's possible. What you should do is just put your datasets into a package, document them there, and then users get easy access to them.

But if you really want to avoid that for some reason, here's how:

library(magrittr)
library(htmltools)
library(tools)
f <- "some.Rd" # Set a filename for an Rd file here
f %>%
parse_Rd %>%
(function(x) capture.output(Rd2HTML(x))) %>%
HTML %>%
browsable

How can I view the source code for a function?

UseMethod("t") is telling you that t() is a (S3) generic function that has methods for different object classes.

The S3 method dispatch system

For S3 classes, you can use the methods function to list the methods for a particular generic function or class.

> methods(t)
[1] t.data.frame t.default t.ts*

Non-visible functions are asterisked
> methods(class="ts")
[1] aggregate.ts as.data.frame.ts cbind.ts* cycle.ts*
[5] diffinv.ts* diff.ts kernapply.ts* lines.ts
[9] monthplot.ts* na.omit.ts* Ops.ts* plot.ts
[13] print.ts time.ts* [<-.ts* [.ts*
[17] t.ts* window<-.ts* window.ts*

Non-visible functions are asterisked

"Non-visible functions are asterisked" means the function is not exported from its package's namespace. You can still view its source code via the ::: function (i.e. stats:::t.ts), or by using getAnywhere(). getAnywhere() is useful because you don't have to know which package the function came from.

> getAnywhere(t.ts)
A single object matching ‘t.ts’ was found
It was found in the following places
registered S3 method for t from namespace stats
namespace:stats
with value

function (x)
{
cl <- oldClass(x)
other <- !(cl %in% c("ts", "mts"))
class(x) <- if (any(other))
cl[other]
attr(x, "tsp") <- NULL
t(x)
}
<bytecode: 0x294e410>
<environment: namespace:stats>

The S4 method dispatch system

The S4 system is a newer method dispatch system and is an alternative to the S3 system. Here is an example of an S4 function:

> library(Matrix)
Loading required package: lattice
> chol2inv
standardGeneric for "chol2inv" defined from package "base"

function (x, ...)
standardGeneric("chol2inv")
<bytecode: 0x000000000eafd790>
<environment: 0x000000000eb06f10>
Methods may be defined for arguments: x
Use showMethods("chol2inv") for currently available ones.

The output already offers a lot of information. standardGeneric is an indicator of an S4 function. The method to see defined S4 methods is offered helpfully:

> showMethods(chol2inv)
Function: chol2inv (package base)
x="ANY"
x="CHMfactor"
x="denseMatrix"
x="diagonalMatrix"
x="dtrMatrix"
x="sparseMatrix"

getMethod can be used to see the source code of one of the methods:

> getMethod("chol2inv", "diagonalMatrix")
Method Definition:

function (x, ...)
{
chk.s(...)
tcrossprod(solve(x))
}
<bytecode: 0x000000000ea2cc70>
<environment: namespace:Matrix>

Signatures:
x
target "diagonalMatrix"
defined "diagonalMatrix"

There are also methods with more complex signatures for each method, for example

require(raster)
showMethods(extract)
Function: extract (package raster)
x="Raster", y="data.frame"
x="Raster", y="Extent"
x="Raster", y="matrix"
x="Raster", y="SpatialLines"
x="Raster", y="SpatialPoints"
x="Raster", y="SpatialPolygons"
x="Raster", y="vector"

To see the source code for one of these methods the entire signature must be supplied, e.g.

getMethod("extract" , signature = c( x = "Raster" , y = "SpatialPolygons") )

It will not suffice to supply the partial signature

getMethod("extract",signature="SpatialPolygons")
#Error in getMethod("extract", signature = "SpatialPolygons") :
# No method found for function "extract" and signature SpatialPolygons

Functions that call unexported functions

In the case of ts.union, .cbindts and .makeNamesTs are unexported functions from the stats namespace. You can view the source code of unexported functions by using the ::: operator or getAnywhere.

> stats:::.makeNamesTs
function (...)
{
l <- as.list(substitute(list(...)))[-1L]
nm <- names(l)
fixup <- if (is.null(nm))
seq_along(l)
else nm == ""
dep <- sapply(l[fixup], function(x) deparse(x)[1L])
if (is.null(nm))
return(dep)
if (any(fixup))
nm[fixup] <- dep
nm
}
<bytecode: 0x38140d0>
<environment: namespace:stats>

Functions that call compiled code

Note that "compiled" does not refer to byte-compiled R code as created by the compiler package. The <bytecode: 0x294e410> line in the above output indicates that the function is byte-compiled, and you can still view the source from the R command line.

Functions that call .C, .Call, .Fortran, .External, .Internal, or .Primitive are calling entry points in compiled code, so you will have to look at sources of the compiled code if you want to fully understand the function. This GitHub mirror of the R source code is a decent place to start. The function pryr::show_c_source can be a useful tool as it will take you directly to a GitHub page for .Internal and .Primitive calls. Packages may use .C, .Call, .Fortran, and .External; but not .Internal or .Primitive, because these are used to call functions built into the R interpreter.

Calls to some of the above functions may use an object instead of a character string to reference the compiled function. In those cases, the object is of class "NativeSymbolInfo", "RegisteredNativeSymbol", or "NativeSymbol"; and printing the object yields useful information. For example, optim calls .External2(C_optimhess, res$par, fn1, gr1, con) (note that's C_optimhess, not "C_optimhess"). optim is in the stats package, so you can type stats:::C_optimhess to see information about the compiled function being called.

Compiled code in a package

If you want to view compiled code in a package, you will need to download/unpack the package source. The installed binaries are not sufficient. A package's source code is available from the same CRAN (or CRAN compatible) repository that the package was originally installed from. The download.packages() function can get the package source for you.

download.packages(pkgs = "Matrix", 
destdir = ".",
type = "source")

This will download the source version of the Matrix package and save the corresponding .tar.gz file in the current directory. Source code for compiled functions can be found in the src directory of the uncompressed and untared file. The uncompressing and untaring step can be done outside of R, or from within R using the untar() function. It is possible to combine the download and expansion step into a single call (note that only one package at a time can be downloaded and unpacked in this way):

untar(download.packages(pkgs = "Matrix",
destdir = ".",
type = "source")[,2])

Alternatively, if the package development is hosted publicly (e.g. via GitHub, R-Forge, or RForge.net), you can probably browse the source code online.

Compiled code in a base package

Certain packages are considered "base" packages. These packages ship with R and their version is locked to the version of R. Examples include base, compiler, stats, and utils. As such, they are not available as separate downloadable packages on CRAN as described above. Rather, they are part of the R source tree in individual package directories under /src/library/. How to access the R source is described in the next section.

Compiled code built into the R interpreter

If you want to view the code built-in to the R interpreter, you will need to download/unpack the R sources; or you can view the sources online via the R Subversion repository or Winston Chang's github mirror.

Uwe Ligges's R news article (PDF) (p. 43) is a good general reference of how to view the source code for .Internal and .Primitive functions. The basic steps are to first look for the function name in src/main/names.c and then search for the "C-entry" name in the files in src/main/*.



Related Topics



Leave a reply



Submit