简体   繁体   English

如何在 Roxygen 文档中重复使用项目描述?

[英]How to re-use item descriptions in Roxygen documentation?

I am documenting many data frames in an R package, either because they are returned by a function or they are included as data.我在 R package 中记录了许多数据帧,因为它们是由 function 返回的,或者它们作为数据包含在内。 Oftentimes, the same column/item needs to be described for many data frames.通常,需要为许多数据帧描述相同的列/项目。 How can I avoid copy/pasting the same item description multiple times?如何避免多次复制/粘贴相同的商品描述?

For example, imagine I am documenting the following data frames in R/data.R :例如,假设我在R/data.R中记录以下数据帧:

#' @name OBJ_1
#' @title Object 1
#' @format A data frame with 401 rows and 6 variables:
#' \describe{
#'   \item{\code{feature_ID}}{character, a very carefully defined description}
#'   \item{\code{col1}}{double, blah}
#'   \item{\code{col2}}{integer, blah}
#'   \item{\code{col3}}{character, blah}
#'   \item{\code{col4}}{integer, blah}
#'   \item{\code{col5}}{character, blah}
#'}   
"OBJ_1"

#' @name OBJ_2
#' @title Object 2
#' @format A data frame with 333 rows and 5 variables:
#' \describe{
#'   \item{\code{feature_ID}}{character, a very carefully defined description}
#'   \item{\code{col6}}{double, blah}
#'   \item{\code{col7}}{integer, blah}
#'   \item{\code{col8}}{character, blah}
#'   \item{\code{col9}}{integer, blah}
#'}   
"OBJ_2"

And similarly I want to document a data frame returned by a function:同样,我想记录一个由 function 返回的数据帧:

#' Some function 
#'
#' @return data frame with one row per feature:
#' \describe{
#'   \item{\code{feature_ID}}{character, a very carefully defined description}
#'   \item{\code{colx}}{blah}
#'   \item{\code{coly}}{blah}
#' }
#' 
#' @export
#' 
myFunction = function(){
  # do stuff
  return(df)
}

In all three cases, feature_ID is defined the same way.在所有三种情况下, feature_ID的定义方式相同。 Is there any way to define feature_ID somewhere once and use an expression in Roxygen that is evaluated to insert the desired definition in the.Rd docs?有没有办法在某处定义feature_ID并使用Roxygen中的表达式来评估以在 .Rd 文档中插入所需的定义?

@eval is a nice way to do this for repeated @param s, but @eval does not work inside another Roxygen tag like @format or @describe . @eval是重复@param的好方法,但@eval在另一个 Roxygen 标记(如@format@describe不起作用

I also tried the dynamic R code solution, but this:我还尝试了动态 R 代码解决方案,但是这个:

feature_ID = function(){
  "character, a very carefully defined description"
}
...
#'   \item{\code{feature_ID}}{`r feature_ID()`}

was evaluated to this in the corresponding.Rd:在相应的.Rd中对此进行了评估:

\item{\code{feature_ID}character, a very carefully defined description)`}

instead of:代替:

\item{\code{feature_ID}{character, a very carefully defined description}

...which seems like a bug more than any intended behavior... ...这似乎比任何预期的行为都更像一个错误...

From this , it sounds like @templateVar name value is promising, but I can't find any documentation about how to actually implement this.这里,听起来@templateVar name value很有希望,但我找不到任何关于如何实际实现它的文档。

I'd appreciate any help.我会很感激任何帮助。 Lots of copy/paste feels like a sin and would be horribly inefficient if/when we end up wanting to change the definition of a variable.大量的复制/粘贴感觉像是一种罪过,如果/当我们最终想要更改变量的定义时,效率会非常低。

It turns out the behavior described using the dynamic R code solution was a bug .事实证明,使用动态 R 代码解决方案描述的行为一个错误 I was using roxygen2 version 7.1.2.我使用的是 roxygen2 7.1.2 版。 Updating to version 7.2.1 resolved the issue and gave me the solution I wanted.更新到 7.2.1 版解决了这个问题,并给了我想要的解决方案。

Here's a complete example of how to dynamically insert text into Roxygen documentation upon rendering.这是一个完整的示例,说明如何在渲染时将文本动态插入到 Roxygen 文档中。

  1. Define frequently used arguments/params/items in an R script, something like R/define_params.R .在 R 脚本中定义常用参数/参数/项目,例如R/define_params.R You don't need to add Roxygen tags, or if you do, add @noRd and do not include @export so that the function is not exported and a corresponding.Rd is not generated.您不需要添加 Roxygen 标记,或者如果需要,添加@noRd并且不包含@export以便不会导出 function 并且不会生成对应的.Rd。
     feature_ID = function(){ "character, a very carefully defined description" } assay = function(){ "character, another description I use a lot" }
  2. Use dynamic R code to insert the string(s) in Roxygen documentation, including inside tags.使用动态 R 代码在 Roxygen 文档中插入字符串,包括内部标签。
     #' Some function #' #' @return data frame with one row per feature: #' \describe{ #' \item{\code{feature_ID}}{`r feature_ID()`} #' \item{\code{colx}}{blah} #' \item{\code{coly}}{blah} #' } #' #' @export #' myFunction = function(){ # do stuff return(df) }
     #' @name OBJ_1 #' @title Object 1 #' @format A data frame with 401 rows and 7 variables: #' \describe{ #' \item{\code{feature_ID}}{`r feature_ID()`} #' \item{\code{assay}}{`r assay()`} #' \item{\code{col1}}{double, blah} #' \item{\code{col2}}{integer, blah} #' \item{\code{col3}}{character, blah} #' \item{\code{col4}}{integer, blah} #' \item{\code{col5}}{character, blah} #'} "OBJ_1"
  3. Generate the docs using roxygen2::roxygenise()使用roxygen2::roxygenise()生成文档
  4. If you look at the.Rd files, you will see that your R code was replaced with the strings defined in the corresponding functions, eg:如果您查看 .Rd 文件,您将看到您的 R 代码已替换为相应函数中定义的字符串,例如:
     ... \item{\code{feature}}{character, a very carefully defined description} \item{\code{assay}}{character, another description I use a lot}}...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM