How to properly document S4 “[” and “[<-“ methods using roxygen?

前端 未结 3 1576
借酒劲吻你
借酒劲吻你 2020-12-14 02:33

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 t

相关标签:
3条回答
  • 2020-12-14 02:57

    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")
        }
    )
    
    0 讨论(0)
  • 2020-12-14 03:02

    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")
      }
    )
    
    0 讨论(0)
  • 2020-12-14 03:10

    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
    })
    
    0 讨论(0)
提交回复
热议问题