如何使用Roxygen2正确记录S4类插槽?

Pau*_*die 302 r class slot s4 roxygen2

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

尽职调查:

@slot在早期的roxygen描述中发现了一个标签. 2008 R-forge邮件列表帖 似乎表明这已经死了,并且没有对@slotroxygen的支持:

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"
)
Run Code Online (Sandbox Code Playgroud)

然而,尽管这个作品,这个\describe,\item用于记录的插槽做法似乎与roxygen(二)其余不一致,在没有@-delimited标签和插槽也没有异议,从无证去roxygenize().它也没有说明记录正在定义的类的继承的一致方法.我想依赖仍然通常使用@import标记工作正常(如果特定插槽需要来自另一个包的非基类).

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

目前似乎有三种选择:

  • A - 分项列表(如上例所示).
  • B - @slot......但是有额外的标签/实施我错过了.我无法让@slot在版本中使用roxygen/roxygen2作为上述示例中逐项列表的替代.同样,上面的例子确实适用于roxygen(2).
  • C - 用于指定插槽的替代标记,例如@param,可以完成相同的操作.

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

Wil*_*ken 29

更新了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
Run Code Online (Sandbox Code Playgroud)

在旁注中,@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"}.}
#'    }
#'  }
Run Code Online (Sandbox Code Playgroud)

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

正如@Brian Diggs指出的那样,这个功能被拉到3.0.0,进一步的讨论在https://github.com/klutometis/roxygen/pull/85

  • 您是否使用过@Brian Diggs的实现?它有用吗?你认为你可以提供一些关于这种方法的细节(因此类似于`@ slot`解决方案).我还没有尝试过,等待(也许懒得)这个待处理的`@ slot`解决方案.我知道这不是问题的提出方式,但根据评论(包括@ hadley's),似乎`@ slot`是"真正的"答案.我同意你的评估,即列表清单(如我的问题)是目前的最佳做法,但希望很快就会被取代. (2认同)

Jor*_*eys 18

如果您要在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
Run Code Online (Sandbox Code Playgroud)


Pau*_*die 14

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类文档. (2认同)