如何正确记录 S4“[”和“[<-“使用氧气的方法?

发布于 2024-10-06 18:47:30 字数 880 浏览 4 评论 0 原文

下面我发布了一个小示例,我想在其中编写文档 S4 类的 “[“ 方法。有人知道如何使用 roxygen 和 S4 正确记录通用 "[" 的方法吗?
构建后检查包时我收到警告(见下文)。

#' An S4 class that stores a string.
#' @slot a contains a string
#' @export
setClass("testClass", 
         representation(a="character"))

#' extract method for testClass
#'
#' @docType methods
#' @rdname extract-methods
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
         function (x, i, j, ..., drop){
             print("void function")
         }
)

包裹检查摘录:

* checking for missing documentation entries ... WARNING
Undocumented S4 methods:
  generic '[' and siglist 'testClass'
All user-level objects in a package (including S4 classes and methods)
should have documentation entries.
See the chapter 'Writing R documentation files' in manual 'Writing R Extensions'.

Below I posted a mini example in which I want do write documentation for
an “[“ method for a S4 class. Does someone know how to properly document a method for the generic "[" using roxygen and S4?
I get a warning when checking the package after building (see below).

#' An S4 class that stores a string.
#' @slot a contains a string
#' @export
setClass("testClass", 
         representation(a="character"))

#' extract method for testClass
#'
#' @docType methods
#' @rdname extract-methods
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
         function (x, i, j, ..., drop){
             print("void function")
         }
)

Excerpt from package check:

* checking for missing documentation entries ... WARNING
Undocumented S4 methods:
  generic '[' and siglist 'testClass'
All user-level objects in a package (including S4 classes and methods)
should have documentation entries.
See the chapter 'Writing R documentation files' in manual 'Writing R Extensions'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不必你懂 2024-10-13 18:47:31

我终于或多或少弄清楚了。至少现在可以用了:

#' An S4 class that stores a string.
#' @slot a contains a string
#' @export
setClass("testClass", 
     representation(a="character"))

#' extract parts of testClass
#'
#' @name [
#' @aliases [,testClass-method
#' @docType methods
#' @rdname extract-methods
#'
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
    function (x, i, j, ..., drop){
       print("void function")
    }
)

I finally figured it out more or less. At least it works now:

#' An S4 class that stores a string.
#' @slot a contains a string
#' @export
setClass("testClass", 
     representation(a="character"))

#' extract parts of testClass
#'
#' @name [
#' @aliases [,testClass-method
#' @docType methods
#' @rdname extract-methods
#'
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
    function (x, i, j, ..., drop){
       print("void function")
    }
)
傲影 2024-10-13 18:47:31

从 roxygen2 > 3.0.0 开始,您不再需要解决方法,只需要:

#' Extract parts of testClass.
#'
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
  function (x, i, j, ..., drop){
    print("void function")
  }
)

As of roxygen2 >3.0.0, you no longer need work arounds and only need:

#' Extract parts of testClass.
#'
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
  function (x, i, j, ..., drop){
    print("void function")
  }
)
九歌凝 2024-10-13 18:47:31

就其价值而言,在替换功能的情况下,您将需要如下所示的内容:

#' An S4 class that stores a list.
#' @export
    setClass("testClass", 
      representation(a="list"))

#' extract parts of testClass
#'
#' @name [
#' @aliases [,testClass-method
#' @docType methods
#' @rdname extract-methods
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
  function (x, i, j, ..., drop) {
     x@a[i]
  }
)

#' replace names of testClass
#'
#' @name [
#' @aliases [<-,testClass-method
#' @docType methods
#' @rdname extract-methods
setReplaceMethod("names", signature(x = "testClass", value = "ANY"), definition = function (x, value) {
  names(x@a) <- value
  x
})

For what it's worth, in the case of a replacement function, you'll want something like the following:

#' An S4 class that stores a list.
#' @export
    setClass("testClass", 
      representation(a="list"))

#' extract parts of testClass
#'
#' @name [
#' @aliases [,testClass-method
#' @docType methods
#' @rdname extract-methods
setMethod("[", signature(x = "testClass", i = "ANY", j="ANY"),
  function (x, i, j, ..., drop) {
     x@a[i]
  }
)

#' replace names of testClass
#'
#' @name [
#' @aliases [<-,testClass-method
#' @docType methods
#' @rdname extract-methods
setReplaceMethod("names", signature(x = "testClass", value = "ANY"), definition = function (x, value) {
  names(x@a) <- value
  x
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文