如何正确记录S4"["和"[< - "方法使用roxygen?

Mar*_*ann 18 r roxygen s4

下面我发布了一个迷你示例,其中我想“[“为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")
         }
)
Run Code Online (Sandbox Code Playgroud)

包检查摘录:

* 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'.
Run Code Online (Sandbox Code Playgroud)

Mar*_*ann 10

我终于或多或少想出来了.至少它现在有效:

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


Max*_*ner 8

对于它的价值,在替换功能的情况下,您需要以下内容:

#' 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
})
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢,@ name在这里是关键,否则你会得到`块中缺少名字'错误.这解决了这个问题. (2认同)

had*_*ley 6

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

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