Background
I am documenting an R package, and I wish the @examples section(s) to include the (commented) output below the code, so the user need not run the examples (in the console, etc.) in order to see the output.
As such, I have typed the output (#> ...) by hand...
#' @name my_topic
#'
#' @title My Topic
#' @description A page for the general topic, under which the specific function(s) will appear.
#' @export
#'
#' @examples
#' # Demo variables available throughout the topic.
#' unnamed_vec <- 1:3
#' unnamed_vec
#' #> [1] 1 2 3
#'
#' named_vec <- c(a = 1, b = 2, c = 3)
#' named_vec
#' #> a b c
#' #> 1 2 3
#' @rdname my_topic
#' @export
#'
#' @param x An \R object.
#'
#' @details `my_fun()` is an alias for [base::names()].
#'
#' @return For `my_fun()`, a `character` vector with the names, or `NULL` if no names are found.
#'
#' @examples
#'
#'
#'
#' # An application of `my_fun()` specifically.
#' my_fun(unnamed_vec)
#' #> NULL
#'
#' my_fun(named_vec)
#' #> [1] "a" "b" "c"
my_fun <- function(x) {
base::names(x)
}
...in order to yield the following page in the manual:
My Topic
Description
A page for the general topic, under which the specific function(s) will appear.
Usage
my_fun(x)Arguments
xAnRobject.Details
my_fun()is an alias forbase::names().Value
For
my_fun(), acharactervector with the names, orNULLif no names are found.Examples
# Demo variables available throughout the topic. unnamed_vec <- 1:3 unnamed_vec #> [1] 1 2 3 named_vec <- c(a = 1, b = 2, c = 3) named_vec #> a b c #> 1 2 3 # An application of `my_fun()` specifically. my_fun(unnamed_vec) #> NULL my_fun(named_vec) #> [1] "a" "b" "c"
Goal
Instead of typing this output (#> ...) by hand, I want it to autogenerate from the executable code, so changes to the commands (my_fun(...)) are dynamically reflected in the output.
I hope to outsource this to R markdown, as with code chunks suggested by roxygen2:
Code chunks
You can insert executable code with
```{r}, just like in knitr documents. For example:#' @title Title #' @details Details #' ```{r lorem} #' 1+1 #' ``` #' @md foo <- function() NULL
Note
I see the website for the purrr package seems to autogenerate output for its Examples
Examples
# Reducing `+` computes the sum of a vector while reducing `*` # computes the product: 1:3 |> reduce(`+`) #> [1] 6 1:10 |> reduce(`*`) #> [1] 3628800
from the executable code under its @examples here:
#' @examples #' # Reducing `+` computes the sum of a vector while reducing `*` #' # computes the product: #' 1:3 |> reduce(`+`) #' 1:10 |> reduce(`*`)
However, this output does not appear on the corresponding Help page in RStudio:
Examples
# Reducing `+` computes the sum of a vector while reducing `*` # computes the product: 1:3 |> reduce(`+`) 1:10 |> reduce(`*`)
Problem
Unfortunately, when I try to implement code chunks under @examples...
#' @examples
#' ```{r}
#' # Demo variables available throughout the topic.
#' unnamed_vec <- 1:3
#' unnamed_vec
#'
#' named_vec <- c(a = 1, b = 2, c = 3)
#' named_vec
#' ```
#' @examples
#' ```{r}
#'
#'
#' # An application of `my_fun()` specifically.
#' my_fun(unnamed_vec)
#'
#' my_fun(named_vec)
#' ```
...I do not get the intended result (below) for the manual:
Examples
# Demo variables available throughout the topic. unnamed_vec <- 1:3 unnamed_vec #> [1] 1 2 3 named_vec <- c(a = 1, b = 2, c = 3) named_vec #> a b c #> 1 2 3 # An application of `my_fun()` specifically. my_fun(unnamed_vec) #> NULL my_fun(named_vec) #> [1] "a" "b" "c"
Instead, my check() triggers the following error:
── R CMD check results ─────────────────────────────────────────────────────────────── pkg 0.0.0.9000 ────
Duration: 18.5s
❯ checking examples ... ERROR
Running examples in ‘my_topic-Ex.R’ failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: my_topic
> ### Title: My Topic
> ### Aliases: my_topic my_fun
>
> ### ** Examples
>
> ```{r}
Error: attempt to use zero-length variable name
Execution halted
❯ checking for unstated dependencies in examples ... WARNING
Warning: parse error in file 'lines':
attempt to use zero-length variable name
1 error ✖ | 1 warning ✖ | 0 notes ✔
Note
I have tried to force markdown with the @md tag...
#' @md
#' @examples
...but the error still occurs.
Unfortunately, I suspect that everything under the @examples tag is automatically treated and executed as pure R code, and never as R markdown. So the result is not the .Rd below...
\examples{
Examples
if{html}{\out{<div class="sourceCode r">}}\preformatted{
# Demo variables available throughout the topic.
unnamed_vec <- 1:3
unnamed_vec
#> [1] 1 2 3
named_vec <- c(a = 1, b = 2, c = 3)
named_vec
#> a b c
#> 1 2 3
# An application of `my_fun()` specifically.
my_fun(unnamed_vec)
#> NULL
my_fun(named_vec)
#> [1] "a" "b" "c"
}\if{html}{\out{</div>}}
}
...but rather an attempt to execute this code in R itself...
```{r}
# Demo variables available throughout the topic.
unnamed_vec <- 1:3
unnamed_vec
named_vec <- c(a = 1, b = 2, c = 3)
named_vec
```
```{r}
# An application of `my_fun()` specifically.
my_fun(unnamed_vec)
my_fun(named_vec)
```
...before rendering (something like) the .Rd below:
\examples{
Examples
\preformatted{
```{r}
# Demo variables available throughout the topic.
unnamed_vec <- 1:3
unnamed_vec
named_vec <- c(a = 1, b = 2, c = 3)
named_vec
```
```{r}
# An application of `my_fun()` specifically.
my_fun(unnamed_vec)
my_fun(named_vec)
```
}
}
Naturally, trying to execute the first line in R...
```{r}
...will yield an error, since the symbol ``` represents a "zero-length variable name":
> ### ** Examples
>
> ```{r}
Error: attempt to use zero-length variable name
Execution halted
Question
What is the canonical way to autogenerate console output for the executable code in @examples?
I would like to avoid hacking together a fake Examples section like so:
#' @md
#' @section Examples:
#' ```{r}
#' # Demo variables available throughout the topic.
#' unnamed_vec <- 1:3
#' unnamed_vec
#'
#' named_vec <- c(a = 1, b = 2, c = 3)
#' named_vec
#' ```
You can roll your own with
parse_RdandRd2exfrom tools. I've written a rough version here, whererdfileandconare paths or text mode connections to Rd files (the input and output) andcommentis the prefix for all output lines inside of\examples{}.Notably, I haven't tried to divert messages or warnings along with example output, but that can in principle be done by establishing suitable calling handlers before evaluating example code.
Taking any Rd file as an example:
You would attach your package with
library, then callwithExOutputwith suitable arguments (e.g., probably ensuring thatcommentstarts with a#):When you accept the default
con = stdout(), the "new" Rd file with example output is just printed in your console (assuming that you haven't diverted it somewhere else):