背景
我正在记录一个R包,希望@examples
部分包括(已注释的)输出,以便用户无需运行示例(在控制台等处)即可看到输出结果。
因此,我手工输入了输出内容(#> ...
)...
#' @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)
}
...为了在手册中得到以下页面:
My Topic
Description
A page for the general topic, under which the specific function(s) will appear.
Usage
my_fun(x)
Arguments
x
AnR
object.Details
my_fun()
is an alias forbase::names()
.Value
For
my_fun()
, acharacter
vector with the names, orNULL
if 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"
目标
我希望不再手动键入此输出 (#> ...
),而是希望能够从可执行代码自动生成它,这样对命令的更改 (my_fun(...)
) 就能动态地反映在输出中。
我希望通过 R Markdown 外包这个任务,就像 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
注意
Examples
# Reducing `+` computes the sum of a vector while reducing `*` # computes the product: 1:3 |> reduce(`+`) #> [1] 6 1:10 |> reduce(`*`) #> [1] 3628800
从可执行代码中的@examples
这里:
#' @examples
#' # Reducing `+` computes the sum of a vector while reducing `*`
#' # computes the product:
#' 1:3 |> reduce(`+`)
#' 1:10 |> reduce(`*`)
然而,这个输出在RStudio相应的帮助页面上并没有出现:不过
Examples
# Reducing `+` computes the sum of a vector while reducing `*` # computes the product: 1:3 |> reduce(`+`) 1:10 |> reduce(`*`)
问题
很不幸,当我尝试在@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)
#' ```
...我对这个手册的意图没有得到预期的结果(如下)。
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"
相反,我的check()
触发了以下错误:
── 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 ✔
注意
我尝试使用@md
标签强制使用Markdown...
#' @md
#' @examples
...但错误仍然存在。
不幸的是,我怀疑在@examples
标签下的所有内容都会自动被视为纯R代码并执行, 而从不作为R markdown。因此结果不是下面的.Rd
...
\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>}}
}
...但是它不是尝试在R 之外执行此代码,而是在R 内部执行...
```{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)
```
在渲染(类似于)下面的.Rd
之前:
\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)
```
}
}
当然,尝试在R中执行第一行...
```{r}
会产生错误,因为符号```
代表“零长度变量名称”:
> ### ** Examples
>
> ```{r}
Error: attempt to use zero-length variable name
Execution halted
问题
有什么规范的方法可以自动生成可执行代码中@examples
的控制台输出吗?
我想要避免像下面那样拼凑一个假的例子部分:
#' @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
#' ```
pkgdown::build_reference
可能是一个不错的起点,但我并不确定是否有一种方法将输出注入到相应的Rd文件中。你可能需要自己处理文本处理的这一部分。 - Mikael Jagan