【问题标题】:How to properly document S4 class slots using Roxygen2?如何使用 Roxygen2 正确记录 S4 类插槽?
【发布时间】:2011-11-14 03:49:04
【问题描述】:

对于使用 roxygen(2) 记录类,指定标题和描述/细节似乎与函数、方法、数据等相同。但是,槽和继承是它们自己的一种动物。在 roxygen2 中记录 S4 类的最佳实践是什么(当前的或计划的)?

尽职调查:

我发现在 roxygen 的早期描述中提到了 @slot 标签。 A 2008 R-forge mailing list post 似乎表明这已经死了, 并且 roxygen 中不支持@slot:

roxygen2 是这样吗?前面提到的帖子建议用户应该使用 LaTeX 标记制作自己的逐项列表。例如。扩展 "character" 类的新 S4 类将按如下方式编码和记录:

#' The title for my S4 class that extends \code{"character"} class.
#'
#' Some details about this class and my plans for it in the body.
#'
#' \describe{
#'    \item{myslot1}{A logical keeping track of something.}
#'
#'    \item{myslot2}{An integer specifying something else.}
#' 
#'    \item{myslot3}{A data.frame holding some data.}
#'  }
#' @name mynewclass-class
#' @rdname mynewclass-class
#' @exportClass mynewclass
setClass("mynewclass",
    representation(myslot1="logical",
        myslot2="integer",
        myslot3="data.frame"),
    contains = "character"
)

然而,虽然这可行,但这种用于记录插槽的 \describe 、\item 方法似乎与 roxygen(2) 的其余部分不一致,因为没有@-delimited 标签和插槽可能没有记录roxygenize() 没有异议。它也没有说明记录正在定义的类的继承的一致方式。我想依赖通常仍然可以使用@import 标记正常工作(如果特定插槽需要来自另一个包的非基类)。

所以,总而言之,roxygen(2) 插槽的当前最佳实践是什么?

目前似乎有三种选择:

  • A -- 分项列表(如上例)。
  • B -- @slot ...但我错过了额外的标签/实现。我无法让@slot 在以下版本中使用 roxygen / roxygen2 它被包含在示例中作为逐项列表的替代品 更多。同样,上面的示例确实适用于 roxygen(2)。
  • C -- 一些用于指定槽的替代标签,如@param,可以完成同样的事情。

我借用/扩展了我在githubroxygen2 开发页面上发布的帖子中的这个问题。

【问题讨论】:

标签: class r s4 slot roxygen2


【解决方案1】:

更新了 Roxygen2 5.0.1 的答案,当前为 6.0.1

对于 S4,现在的最佳做法是使用 @slot 标签记录:

#' The title for my S4 class that extends \code{"character"} class.
#'
#' Some details about this class and my plans for it in the body.
#'
#' @slot myslot1 A logical keeping track of something.
#' @slot myslot2 An integer specifying something else.
#' @slot myslot3 A data.frame holding some data.
#'
#' @name mynewclass-class
#' @rdname mynewclass-class
#' @export

顺便说一句,@exportClass 仅在某些情况下是必需的,现在导出函数的一般方法是使用@export。您也不必导出类,除非您希望其他包能够扩展该类。

另见http://r-pkgs.had.co.nz/namespace.html#exports

更新了 Roygen2 3.0.0 的答案,当前为 5.0.1。

对于 S4,最佳实践是以下形式的文档:

#'  \section{Slots}{
#'    \describe{
#'      \item{\code{a}:}{Object of class \code{"numeric"}.}
#'      \item{\code{b}:}{Object of class \code{"character"}.}
#'    }
#'  }

这与对象内部作为列表的槽的内部表示是一致的。正如您所指出的,这种语法与其他行不同,我们可能希望将来有一个更强大的解决方案,其中包含继承知识——但今天不存在。

正如@Brian Diggs 所指出的,此功能已被纳入 3.0.0,进一步讨论https://github.com/klutometis/roxygen/pull/85

【讨论】:

  • 你使用过@Brian Diggs 的实现吗?它有效吗?您认为您可以提供有关该方法的一些详细信息(因此类似于@slot 解决方案)。我还没来得及尝试,等待(也许是懒惰地)等待这个待处理的@slot 解决方案。我知道这不是问题的提出方式,但基于 cmets(包括@hadley's),@slot 似乎是“真正的”答案。我同意您的评估,即逐项列表(如我的问题)是当前的最佳实践,但希望很快被替换。
【解决方案2】:

如果您在 Rd 文件本身中记录插槽,Full Decent 提供的解决方案是可以的。在使用roxygen2 时,可以使用标签@section 与\describe 做基本相同的事情。一个例子:

#' The EXAMPLE class
#'
#' This class contains an example. This line goes into the description
#'
#' This line and the next ones go into the details.
#' This line thus appears in the details as well.
#'
#'@section Slots: 
#'  \describe{
#'    \item{\code{slot1}:}{Matrix of class \code{"numeric"}, containing data from slot1}
#'    \item{\code{slot2}:}{Object of class \code{"character"}, containing data that needs to go in slot2.}
#'  }
#'
#' @note You can still add notes
#' @name EXAMPLE 
#' @rdname EXAMPLE
#' @aliases EXAMPLE-class
#' @exportClass EXAMPLE
#' @author Joris Meys

【讨论】:

    【解决方案3】:

    roxygen2 v4.1+ 和 Hadley 的最新文档:

    http://r-pkgs.had.co.nz/man.html#man-classes

    我还没有为 RC 尝试过,但它现在适用于 S4。

    以前

    看起来 Roxygen2 3.0+ 版本完全支持 S4 类插槽:

    http://blog.rstudio.org/2013/12/09/roxygen2-3-0-0/

    “使用 roxygen2 记录您的 S4 类、S4 方法和 RC 类 - 您可以安全地删除使用 @alias 和 @usage 的变通办法,而只需依靠 roxygen2 来做正确的事情。”

    【讨论】:

    • @slot 完美运行,您也可以使用它(或@field)来伪造 S3 类的文档。
    猜你喜欢
    • 2011-11-13
    • 2011-11-04
    • 1970-01-01
    • 2011-05-22
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多