首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Roxygen2正确地记录S4类插槽?

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

Stack Overflow用户
提问于 2011-09-10 06:52:17
回答 3查看 14K关注 0票数 311

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

尽职调查:

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

这对roxygen2来说是真的吗?前面提到的帖子建议用户应该使用LaTeX标记来创建自己的分项列表。例如,扩展"character"类的新S4类的编码和文档将如下所示:

代码语言:javascript
复制
#' 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,可以完成相同的任务。

我借用/扩展了我在github上的roxygen2开发页面上的一篇帖子。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-12-10 09:01:08

roxygen2 v4.1+和哈德利的最新文档:

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来做正确的事情。

票数 16
EN

Stack Overflow用户

发布于 2012-06-09 05:53:04

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

对于S4,现在的最佳实践是使用@slot标记进行记录:

代码语言:javascript
复制
#' 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,最佳实践是采用以下形式的文档:

代码语言:javascript
复制
#'  \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上进行了进一步的讨论

票数 31
EN

Stack Overflow用户

发布于 2013-01-09 21:49:11

如果你要在Rd文件本身中记录插槽,Full Full提供的解决方案是可以的。在使用roxygen2时,您可以使用标记@section\describe执行基本相同的操作。举个例子:

代码语言:javascript
复制
#' 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
票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7368262

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档