Title: | Functions for Diffusing Function Documentations into 'Roxygen' Comments |
---|---|
Description: | Efficient diffusing of content across function documentations. Sections, parameters or dot parameters are extracted from function documentations and turned into valid Rd character strings, which are ready to diffuse into the 'roxygen' comments of another function by inserting inline code. |
Authors: | Xiurui Zhu [aut, cre] |
Maintainer: | Xiurui Zhu <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.2.2 |
Built: | 2025-01-15 05:10:44 UTC |
Source: | https://github.com/zhuxr11/roclang |
The 'roclang' package facilitates efficient diffusing of content across function documentations. Sections, parameters or dot parameters are extracted from function documentations and turned into valid Rd character strings, which are ready to diffuse into the 'roxygen' comments of another function by inserting inline code.
Text extraction and manipulation function: extract_roc_text
.
Rd evaluation and compilation function: roc_eval_text
.
Change log:
0.1.1 Xiurui Zhu - Initiate the document.
Xiurui Zhu
extract_roc_text
cites sections or parameters from a function documentation
in the syntax of @inherit
, @inheritSection
, @inheritParams
or @inheritDotParams
tag
from roxygen2
package. See details about how to use this function.
extract_roc_text( fun, type = c("general", "section", "param", "dot_params"), select = NULL, capitalize = NA )
extract_roc_text( fun, type = c("general", "section", "param", "dot_params"), select = NULL, capitalize = NA )
fun |
Function or character (of length 1L) indicating function name. |
|||||||||||
type |
Type of extraction. Please choose one from the following table
according to the
|
|||||||||||
select |
Selection of extraction based on
|
|||||||||||
capitalize |
Logical (of length 1L) indicating whether the first letter of the return should be capitalized.
Default to |
To diffuse the function output into roxygen2
comments,
one may write the function documentation with inline code like this:
#' Diffusion of function documentation with inline code #' #' @return Same as \code{\link[stats]{lm}}: #' `r extract_roc_text(stats::lm, type = "general", select = "return")` my_fun <- function() {}
or with code block like this:
#' Diffusion of function documentation with code block #' #' @param lm_arg Named list of #' ```{r} #' extract_roc_text(stats::lm, #' type = "dot_params", #' select = c("-formula", "-data"), #' capitalize = FALSE) #' ``` my_fun <- function(lm_arg) {}
Character (of length 1L) as a valid Rd character string to diffuse into roxygen2
comments.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
0.1.1 Xiurui Zhu - Change the default of capitalize
from TRUE
to NA
.
0.1.1 Xiurui Zhu - Improve code security in evaluating the formal arguments of fun
.
0.2.0 Xiurui Zhu - Make changes for roxygen2 > 7.1.2
while keeping compatibility.
Xiurui Zhu
# Inherit a standard section, and leave the first letter as is cat( extract_roc_text(stats::lm, type = "general", select = "description", capitalize = NA) ) # Inherit a self-defined section, and capitalize the first letter cat( extract_roc_text(stats::lm, type = "section", select = "Using time series", capitalize = TRUE) ) # Inherit a parameter, and diffuse it into text cat( paste0( "Here is the `formula` argument of `stats::lm`, defined as: ", extract_roc_text(stats::lm, type = "param", select = "formula", capitalize = FALSE) ) ) # Inherit a set of dot params, and diffuse it into text cat( paste0( "`lm_arg` is a named list of ", extract_roc_text(stats::lm, type = "dot_params", select = c("-formula", "-data"), capitalize = FALSE) ) )
# Inherit a standard section, and leave the first letter as is cat( extract_roc_text(stats::lm, type = "general", select = "description", capitalize = NA) ) # Inherit a self-defined section, and capitalize the first letter cat( extract_roc_text(stats::lm, type = "section", select = "Using time series", capitalize = TRUE) ) # Inherit a parameter, and diffuse it into text cat( paste0( "Here is the `formula` argument of `stats::lm`, defined as: ", extract_roc_text(stats::lm, type = "param", select = "formula", capitalize = FALSE) ) ) # Inherit a set of dot params, and diffuse it into text cat( paste0( "`lm_arg` is a named list of ", extract_roc_text(stats::lm, type = "dot_params", select = c("-formula", "-data"), capitalize = FALSE) ) )
roc_eval_text
is an upgraded version of roc_proc_text
that evaluates inline and block code before generating Rd.
roc_eval_text(roclet, input)
roc_eval_text(roclet, input)
roclet |
Name of roclet to use for processing. |
input |
Source string |
List with names as fun_name.Rd
, where each element is the RoxyTopic
for
the corresponding function, same as the return of roc_proc_text
.
Change log:
0.1.0 Xiurui Zhu - Initiate the function.
Xiurui Zhu
# Formulate a text version of a function with documentation fun_text <- ' #\' \\code{iris} is a `r nrow(iris)`-row matrix. #\' #\' \\code{iris} matrix has #\' ```{r results="hold"} #\' ncol(iris) #\' ``` #\' columns. print_iris <- function() iris ' # Parse the 'roxygen' comments to Rd documentation roc_eval_text(roxygen2::rd_roclet(), fun_text)[[1L]]
# Formulate a text version of a function with documentation fun_text <- ' #\' \\code{iris} is a `r nrow(iris)`-row matrix. #\' #\' \\code{iris} matrix has #\' ```{r results="hold"} #\' ncol(iris) #\' ``` #\' columns. print_iris <- function() iris ' # Parse the 'roxygen' comments to Rd documentation roc_eval_text(roxygen2::rd_roclet(), fun_text)[[1L]]