| Title: | Efficient Manipulation of 'Rd' Files and Help Topics |
|---|---|
| Description: | Provides fast, cached lookup of help topics and aliases across installed, source, and in-development packages, plus efficient retrieval of parsed 'Rd' ('R' documentation) objects. Per-package indexes are built once and cached, making repeated retrieval cheap enough to call in a tight loop. |
| Authors: | Hadley Wickham [aut, cre, cph] (ORCID: <https://orcid.org/0000-0003-4757-117X>), Posit Software, PBC [cph, fnd] (ROR: <https://ror.org/03wc8by49>) |
| Maintainer: | Hadley Wickham <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-07-17 07:42:25 UTC |
| Source: | https://github.com/r-lib/rdtools |
Clears the cached topic index, parsed Rd objects, and dependency search
set for package, or for every package when package is NULL. This will generally be called
automatically by roxygen2 for source packages. Installed package indexes
are automatically reset when their namespace is unloaded.
pkg_cache_reset(package = NULL)pkg_cache_reset(package = NULL)
package |
A package name, or a path to the source directory of a
package. Use |
NULL, invisibly.
head(pkg_topics("stats")) pkg_cache_reset("stats") # Reset every cached index pkg_cache_reset()head(pkg_topics("stats")) pkg_cache_reset("stats") # Reset every cached index pkg_cache_reset()
Loads the macros declared by the package's RdMacros field and
man/macros/ directory, layered over R's system macros. This reproduces the
macro environment used when installing a package.
pkg_macros(package)pkg_macros(package)
package |
A package name, or a path to the source directory of a package. |
An environment containing the Rd macro definitions.
macros <- pkg_macros("stats") head(ls(macros))macros <- pkg_macros("stats") head(ls(macros))
pkg_search_attached() returns the packages a bare ?topic can see: every
attached package, in search path order, followed by the base packages that
are always available. This is the default search set for topic_find().
pkg_search_base() returns the base packages that are always searched.
pkg_search_attached() pkg_search_base()pkg_search_attached() pkg_search_base()
A character vector of package names.
pkg_search_attached() pkg_search_base()pkg_search_attached() pkg_search_base()
pkg_search_deps() returns the packages whose topics package's
documentation can link to: the packages declared in its Depends,
Imports, and Suggests fields, followed by the base packages, which are always available. The result is
cached alongside the package's topic index, so it remains valid until
pkg_cache_reset() is called.
pkg_search_deps(package)pkg_search_deps(package)
package |
A package name, or a path to the source directory of a package. |
A character vector of package names.
pkg_search_deps("stats")pkg_search_deps("stats")
pkg_topics() returns a named character vector mapping every alias
(i.e. everything you can type after ?) to the name of the Rd file
(without extension) that documents it. It understands three kinds of
package:
Installed packages, read from their help/aliases.rds index.
In-development packages loaded with pkgload::load_all(),
indexed from the \alias{} commands in their source man/ directory.
Source packages, when package is a path to a package directory
rather than a name.
Indexes remain cached until pkg_cache_reset() is called. Installed package
indexes are also reset automatically when their namespace is unloaded.
For source packages, \alias{} extraction is line-based: any number of
aliases may appear anywhere on a line, but an alias must open and close
on the same line, and aliases produced by Rd macros are not seen.
pkg_topics(package)pkg_topics(package)
package |
A package name, or a path to the source directory of a package. |
A named character vector mapping alias to Rd file name.
head(pkg_topics("stats"))head(pkg_topics("stats"))
Checks whether any of packages has an exact alias matching topic.
topic_exists(topic, packages = pkg_search_attached())topic_exists(topic, packages = pkg_search_attached())
topic |
A single string naming an alias, matched exactly.
Use |
packages |
A character vector of package names (and/or source
package paths) to search, in order. Defaults to |
A single TRUE or FALSE.
topic_exists("rnorm", "stats") topic_exists("rnorm", c("base", "stats")) topic_exists("median") topic_exists("not-a-topic", "stats")topic_exists("rnorm", "stats") topic_exists("rnorm", c("base", "stats")) topic_exists("median") topic_exists("not-a-topic", "stats")
topic_find() looks topic up in each of packages in order and returns
the first hit. topic_find_all() returns every hit. Lookups use the cached
per-package indexes built by pkg_topics(), so scanning even a long search
set is cheap.
topic_find(topic, packages = pkg_search_attached()) topic_find_all(topic, packages = pkg_search_attached())topic_find(topic, packages = pkg_search_attached()) topic_find_all(topic, packages = pkg_search_attached())
topic |
A single string naming an alias, matched exactly.
Use |
packages |
A character vector of package names (and/or source
package paths) to search, in order. Defaults to |
topic_find() returns NULL if the topic isn't found; otherwise a
list with elements:
package: the package that documents the topic.
file: the name of the Rd file (without extension).
topic_find_all() returns a data frame with columns package and
file, containing one row per hit (and no rows if the topic isn't
found).
topic_find("rnorm") topic_find("mean", c("stats", "base")) topic_find_all("plot", c("graphics", "base")) topic_find("no-such-topic")topic_find("rnorm") topic_find("mean", c("stats", "base")) topic_find_all("plot", c("graphics", "base")) topic_find("no-such-topic")
Determines which package supplies the object associated with a documented topic. This resolves re-exported functions and imported objects to the package where they originate. Results are cached alongside the package's topic index, so repeated lookups are cheap.
topic_origin(topic, package)topic_origin(topic, package)
topic |
A single string naming a topic. |
package |
A package name. |
A single package name.
topic_origin("rnorm", "stats")topic_origin("rnorm", "stats")
Determines whether a topic needs a package qualifier when linking from the
documentation of another package. The from package is checked first,
followed by packages, and then the base packages. Re-exported objects
are attributed to their original package. Results are cached alongside the
from package's topic index, so repeated lookups are cheap.
topic_qualifier(topic, from, packages = character())topic_qualifier(topic, from, packages = character())
topic |
A single string naming an alias, matched exactly. |
from |
The name or source directory of the package you are linking
from. If it documents |
packages |
A character vector of additional packages to search,
typically the dependencies of |
NULL if the topic isn't documented by from, packages, or the base
packages.
NA_character_ if the topic needs no qualifier because it's documented
by from or a base package.
Otherwise, the package name(s) that could qualify the topic: usually one, but more if the topic is ambiguous, in which case it's up to the caller to decide how to respond.
topic_qualifier("rnorm", "stats")topic_qualifier("rnorm", "stats")
Retrieves the parsed Rd object documenting topic in package. For
installed packages the topic is fetched lazily from the package's help
database (no parsing needed); for source and in-development packages the
Rd file is parsed with tools::parse_Rd(), with the package's Rd macros
loaded. Results are cached per topic, so repeated access (e.g. roxygen2
inheriting several fields from one topic) only pays once.
topic_rd(topic, package)topic_rd(topic, package)
topic |
A single string. |
package |
A package name, or a path to the source directory of a package. |
An Rd object: a recursive structure of class "Rd", as returned
by tools::parse_Rd(). Returns NULL if the package or topic doesn't
exist; use topic_exists() first if you need to distinguish that from
other problems.
rd <- topic_rd("rnorm", "stats") class(rd)rd <- topic_rd("rnorm", "stats") class(rd)
Returns the path to the .Rd file that documents topic. Rd files only
exist on disk for source and in-development packages; installed packages
store their documentation in a binary database, so topic_rd_path()
returns NULL for them. Use topic_rd() if you want the parsed contents
regardless of where they are stored.
topic_rd_path(topic, package)topic_rd_path(topic, package)
topic |
A single string. |
package |
A package name, or a path to the source directory of a package. |
The path to a .Rd file, or NULL if the package or topic
doesn't exist, or if the package doesn't have Rd files on disk.
# Rd files only exist on disk for source packages, so make a minimal one pkg <- tempfile() dir.create(file.path(pkg, "man"), recursive = TRUE) writeLines("Package: demo", file.path(pkg, "DESCRIPTION")) writeLines( c("\\name{foo}", "\\alias{foo}", "\\title{Foo}", "\\description{Foo.}"), file.path(pkg, "man", "foo.Rd") ) topic_rd_path("foo", pkg)# Rd files only exist on disk for source packages, so make a minimal one pkg <- tempfile() dir.create(file.path(pkg, "man"), recursive = TRUE) writeLines("Package: demo", file.path(pkg, "DESCRIPTION")) writeLines( c("\\name{foo}", "\\alias{foo}", "\\title{Foo}", "\\description{Foo.}"), file.path(pkg, "man", "foo.Rd") ) topic_rd_path("foo", pkg)
Splits "pkg::topic" (or "pkg:::topic") into its package and topic
components. The prefix is only treated as a qualifier when it is a
syntactically valid package name, so aliases that merely contain ::
(e.g. S7 method aliases like "speak,foo::Dog-method") are left intact.
topic_split(topic)topic_split(topic)
topic |
A single string. |
A list with elements package (a string, or NULL if the
topic is unqualified) and topic.
topic_split("stats::rnorm") topic_split("rnorm") topic_split("speak,foo::Dog-method")topic_split("stats::rnorm") topic_split("rnorm") topic_split("speak,foo::Dog-method")