Title: | Utilities for Processing Rd Objects and Files |
---|---|
Description: | Provides utilities for processing Rd objects and files. Extract argument descriptions and other parts of the help pages of functions. |
Authors: | Georgi N. Boshnakov [aut, cre], R Core Team [cph] (Extracted some non-exported functions from base R) |
Maintainer: | Georgi N. Boshnakov <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.4.12 |
Built: | 2024-12-15 07:18:36 UTC |
Source: | CRAN |
Provides utilities for processing Rd objects and files. Extract argument descriptions and other parts of the help pages of functions. The main purpose of the functions is to facilitate extraction of descriptions of function arguments for presentation of simplified usage descriptions.
Georgi N. Boshnakov
Maintainer: Georgi N. Boshnakov <[email protected]>
Return all or selected sections of a help topic as an Rd object. The help topic may be an Rd object, a character string (for the help function), or the value returned by the help function.
Rd_fun(x, topic, pkgname = "", help_type = "text", verbose = FALSE, try.all.packages = FALSE, keep_section = TRUE)
Rd_fun(x, topic, pkgname = "", help_type = "text", verbose = FALSE, try.all.packages = FALSE, keep_section = TRUE)
x |
the help object. Its class may be "Rd", "character", or "help_files_with_topic". |
topic |
unused, see Details |
pkgname |
unused, see Details |
help_type |
type of help, see Details and |
verbose |
logical value, see |
try.all.packages |
logical value, see |
keep_section |
the section(s) to keep. If it is a
character vector of length at least one, the sections listed in it
(plus |
If the class of x
is neither "Rd" nor "help_files_with_topic",
x
is assumed to be appropriate for a call to
help
. The call is made to obtain an object of class
"help_files_with_topic", which is then processed as below.
Arguments help_type
, verbose
and try.all.packages
are used only in this case.
If the class of x
is "help_files_with_topic"
(usually the result of a call to help
), then an Rd object is
obtained using tools:::fetchRdDB
.
The Rd object (x
itself or the one obtained as described above)
is examined and sections are retained or dropped as specified by
argument keep_section
.
Sections \title
and \name
are always kept in the
returned value since otherwise the Rd object is considered invalid by
(some of?) the system functions.
an Rd object
Note that help
works with ‘installed’ help. So, when the Rd
object is obtained via a call to help it will not necessarilly
be the one that would be obtained from the original Rd file if that
contains \Sexpr
instructions with stage=build
or
stage=install
optional argument. This is not a problem for the
intended purpose of this package to allow for extraction of pieces of
the help for selective display and related run-time actions.
For manipulation of source Rd files one can supply an Rd object
obtained by parse_Rd
-ying it.
FIXME: I wrote this function in a hurry when it turned out that the help system has changed in R version 2.10, needs clean up.
todo:
In recent versions of R, help
may return more than one file
(see paths
in this function's source), this needs to be
handled.
Georgi N. Boshnakov
# 1st arg is name of a function Rd_fun("data.frame",keep_section="\\arguments") Rd_fun("seq",keep_section="\\arguments") # 1st arg is the value of a call to help() h1 <- help("seq") class(h1) Rd_fun(h1,keep_section="\\title") # note: in Rd file the number of Rd_fun(h1,keep_section="\\arguments") # backslashes is twice that in # the rendered doc.
# 1st arg is name of a function Rd_fun("data.frame",keep_section="\\arguments") Rd_fun("seq",keep_section="\\arguments") # 1st arg is the value of a call to help() h1 <- help("seq") class(h1) Rd_fun(h1,keep_section="\\title") # note: in Rd file the number of Rd_fun(h1,keep_section="\\arguments") # backslashes is twice that in # the rendered doc.
Extract selected help sections as text.
Rd_help2txt(x, topic, pkgname = "", help_type = "text", verbose = FALSE, try.all.packages = FALSE, keep_section = TRUE, omit_sec_header = FALSE)
Rd_help2txt(x, topic, pkgname = "", help_type = "text", verbose = FALSE, try.all.packages = FALSE, keep_section = TRUE, omit_sec_header = FALSE)
x |
the help object. Its class may be "Rd", string or "help_files_with_topic". |
topic |
passed on to |
pkgname |
passed on to |
help_type |
passed on to |
verbose |
passed on to |
try.all.packages |
passed on to |
keep_section |
the section to keep. If it is a
character vector of length at least one, the sections listed in it
(plus |
omit_sec_header |
whether to omit or not the section header |
Basically, this function calls Rd_fun
to get an Rd object
containing the required help sections, then converts them to text with
tools::Rd2txt
. At this point however unwanted sections may be
present since tools::Rd2txt
requires \title
and \name
.
If \title
is not an element of keep_section
, it should be
dropped. Other header information is dropped if omit_sec_header
= TRUE
. The way this is done is crude and based on inspection. It
would be better done using the Rd object but then I might need to,
effectively reprogram Rd2txt
.
FIXME: The above was done for version R-2.10 (I think), see if a more
modular version is available in current versions of R.
Also, it is tested only with help_type="text"
.
FIXME: Arguments whose description is marked "passed on to
Rd_fun
" could be replaced by a "..." argument.
A character vector containing the text of the selected help sections.
In R-2.12.0 the function tools::Rd2txt
acquired a fragment
argument. So, tools::Rd2txt
now works with fragments and can be
used directly in many cases.
Georgi N. Boshnakov
# 1st arg is the name of a function Rd_help2txt("data.frame",keep_section="\\arguments") Rd_help2txt("seq",keep_section="\\examples") Rd_help2txt("seq",keep_section="\\examples",omit_sec_header=TRUE)
# 1st arg is the name of a function Rd_help2txt("data.frame",keep_section="\\arguments") Rd_help2txt("seq",keep_section="\\examples") Rd_help2txt("seq",keep_section="\\examples",omit_sec_header=TRUE)
Wrap an object so that it can be used as a section element of an Rd object.
Rdo_set_sectag(s,sectag,eltag) Rd_title(s) Rd_name(s) Rd_args(s)
Rdo_set_sectag(s,sectag,eltag) Rd_title(s) Rd_name(s) Rd_args(s)
s |
the object to be wrapped, often a string, see Details |
sectag |
the section tag, a string |
eltag |
the element tag, a string |
Rdo_set_sectag
sets atrribute "Rd_tag" of the object s
to eltag
, then wraps s
in list()
with "Rd_tag"
attribute sectag
.
The remaining functions provide one-argument access for some
frequently used special cases. eltag
is "TEXT" for
Rd_title
and "VERB" for Rd_name
and Rd_args
. The
values of sectag
are \title
, \name
and
\arguments
, respectively.
A tagged list as described in Details.
Georgi N. Boshnakov
Rd_title("My seq") Rd_name("myseq") "a" %in% letters # to do: more examples
Rd_title("My seq") Rd_name("myseq") "a" %in% letters # to do: more examples
Extract help descriptions of one or more arguments of a function and return them as a string.
Rdo_args2txt(rdo, arg, title = "Hhh", name = "Aa", type = "text")
Rdo_args2txt(rdo, arg, title = "Hhh", name = "Aa", type = "text")
rdo |
the documentation for the topic, typically an Rd object but
may be anything that |
arg |
name(s) of argument(s) to describe, a character vector, see also Details section |
title |
Title, a string |
name |
name, a string |
type |
type of the help, defaults to "text" |
The title
and name
fields are there, since descriptions of
arguments usually do not use the same header as the description of
the corresponding function.
The current defaults show that this is still not finished.
A string (character vector of length one).
Georgi N. Boshnakov
# ?seq cat(Rdo_args2txt("seq", c("by", "..."))) cat(Rdo_args2txt("seq", c("from", "by")))
# ?seq cat(Rdo_args2txt("seq", c("by", "..."))) cat(Rdo_args2txt("seq", c("from", "by")))
Collect the descriptions of the arguments of a function in a named list with one element per argument.
Rdo_args2txt_list(x, arg, ...)
Rdo_args2txt_list(x, arg, ...)
x |
help object, may be any of the types that
|
arg |
A character vector naming the arguments to describe. If |
... |
additional arguments to pass to |
If several arguments are described in a single documentation entry, then the whole text of the entry is given for each of the arguments.
A named list with one entry (a string) for each of the requested arguments.
Georgi N. Boshnakov
# each arg always gets an individual entry in the list; # compare: Rdo_args2txt_list("seq", c("from", "to", "by")) # to: cat(Rdo_args2txt("seq", c("from", "to", "by")))
# each arg always gets an individual entry in the list; # compare: Rdo_args2txt_list("seq", c("from", "to", "by")) # to: cat(Rdo_args2txt("seq", c("from", "to", "by")))
Create basic Rd objects with fields title, name and arguments.
Rdo_create(arguments, title = "Dummy title", name = "dummy name") Rdo_empty(rdtag)
Rdo_create(arguments, title = "Dummy title", name = "dummy name") Rdo_empty(rdtag)
arguments |
The |
title |
the title, a string |
name |
the name, atring |
rdtag |
a value for "Rd_tag", a string. |
Rdo_create
is an auxiliary function used to prepare arguments
for a
call to Rd_help2txt
since the latter works on Rd objects or text but
not on Rd sections.
Rdo_empty
creates an empty object of class "Rd" if
rdtag
is missing. If rdtag
is supplied the object is a
list with attribute "Rd_tag" set to rdtag
.
an Rd object or a list with attribute "Rd_tag".
Georgi N. Boshnakov
require(tools) # for Rd2txt a1 <- Rdo_get_args("seq") a1 Rdo_create(a1) Rd2txt(Rdo_create(a1)) a2 <- Rdo_get_args("seq", c("from", "to", "by")) a2 Rdo_create(a2) Rd2txt(Rdo_create(a2)) Rdo_empty() class(Rdo_empty()) str(Rdo_empty())
require(tools) # for Rd2txt a1 <- Rdo_get_args("seq") a1 Rdo_create(a1) Rd2txt(Rdo_create(a1)) a2 <- Rdo_get_args("seq", c("from", "to", "by")) a2 Rdo_create(a2) Rd2txt(Rdo_create(a2)) Rdo_empty() class(Rdo_empty()) str(Rdo_empty())
Extract argument description from a help topic.
Rdo_get_args(rd, args, ...) Rdo_get_arg(rd, arg)
Rdo_get_args(rd, args, ...) Rdo_get_arg(rd, arg)
rd |
the documentation for the topic, typically an Rd object but
may be anything that |
arg |
an argument name, a string |
args |
names of arguments to describe, a character vector, see also Details section |
... |
not used |
If arguments
is missing, descriptions of all arguments are
returned.
Effort is made to handle the case when two or more arguments are
described in a single entry. In that case it is not possible to
disentangle the description automatically. So, the description is
returned as is. Also, only one copy of the description is returned,
see the examples with the from
and to
arguments of
function seq
.
The ...
argument is handled, as well, give it as the string
...
in args
.
Rdo_get_arg
simply calls Rdo_get_args
and returns the
first element of its value. This means that arg
is expected to
be of length one, but this is not enforced. Note also that
Rdo_get_arg
is not completely equivalent to calling
Rdo_get_args
with length(args)=1
.
For Rdo_get_args
, an Rd fragment representing the (part of)
help section \arguments
containing descriptions of the requested
arguments.
For Rdo_get_arg
an Rd fragment representing the help for a
single argument.
Georgi N. Boshnakov
h1 <- help("seq") Rdo_get_args(h1) Rdo_get_args(h1,"by") Rdo_get_args(h1,"length.out") Rdo_get_args(h1,"...") Rdo_get_args(h1,"from") Rdo_get_args(h1,c("from","by")) Rdo_get_args(h1,c("from", "to")) Rdo_get_args("seq") Rdo_get_args("seq","by") Rdo_get_args("seq","length.out") Rdo_get_args("seq","...") Rdo_get_args("seq","from") Rdo_get_args("seq",c("from","by")) Rdo_get_args("seq",c("from", "to"))
h1 <- help("seq") Rdo_get_args(h1) Rdo_get_args(h1,"by") Rdo_get_args(h1,"length.out") Rdo_get_args(h1,"...") Rdo_get_args(h1,"from") Rdo_get_args(h1,c("from","by")) Rdo_get_args(h1,c("from", "to")) Rdo_get_args("seq") Rdo_get_args("seq","by") Rdo_get_args("seq","length.out") Rdo_get_args("seq","...") Rdo_get_args("seq","from") Rdo_get_args("seq",c("from","by")) Rdo_get_args("seq",c("from", "to"))
Extract a section element from an Rd object or Rd fragment.
Rdo_section(rdo, sec)
Rdo_section(rdo, sec)
rdo |
an Rd object or fragment |
sec |
the required section, a string |
If the class or the "Rd_tag" attribute of rdo
is "Rd" the
required section is extracted. Otherwise, if this attribute is equal to
sec
, then rdo
is returned.
In all other cases it is assumed that rdo
is the contents of
the required section, its "Rd_tag" attribute is set to sec
and
returned without further modification.
An Rd fragment for use as a section element of an Rd object
This function is intended for use by other functions which work with Rd objects.
Georgi N. Boshnakov