Roxygen2 - how to properly document S3 methods

前端 未结 2 1625
南笙
南笙 2020-11-27 14:14

I\'ve read the Roxygen2 PDF as well as this site and I\'m lost about the difference between @method @S3method @export and how you use these to properly document S3 methods.

相关标签:
2条回答
  • 2020-11-27 14:41

    As of roxygen2 >3.0.0, you only need @export:

    #' A description of MyHappyFunction
    #'
    #' A details of MyHappyFunction
    #'
    #' @title MyHappyFunction: The my happy function
    #' @param x numeric number
    #' @param ... other arguments
    #' @examples
    #' a <- 1
    #' class(a) <- "lm"
    #' MyHappyFunction(a)
    #' @export
    MyHappyFunction <- function(x, ...){
      UseMethod("MyHappyFunction")
    }
    
    #' @rdname MyHappyFunction
    #' @export
    MyHappyFunction.lm = function(x, ...) {
      # do some magic
    }
    
    #' @rdname MyHappyFunction
    #' @export
    MyHappyFunction.default = function(x, ...) {
      # do some magic
    }
    

    But since you're not actually documenting the methods, the following is sufficient:

    #' A description of MyHappyFunction
    #'
    #' A details of MyHappyFunction
    #'
    #' @title MyHappyFunction: The my happy function
    #' @param x numeric number
    #' @param ... other arguments
    #' @examples
    #' a <- 1
    #' class(a) <- "lm"
    #' MyHappyFunction(a)
    #' @export
    MyHappyFunction <- function(x, ...){
      UseMethod("MyHappyFunction")
    }
    
    #' @export
    MyHappyFunction.lm = function(x, ...) {
      # do some magic
    }
    
    #' @export
    MyHappyFunction.default = function(x, ...) {
      # do some magic
    }
    
    0 讨论(0)
  • 2020-11-27 14:48

    The @method tag generates \method entries in the \usage field in Rd files.

    The @S3method tag generates S3method() entries in the NAMESPACE file.

    The @export tag generates export() entries in the NAMESPACE file.

    Here is my example:

    #' A description of MyHappyFunction
    #'
    #' A details of MyHappyFunction
    #'
    #' @title MyHappyFunction: The my happy function
    #' @param x numeric number
    #' @param ... other arguments
    #' @examples
    #' a <- 1
    #' class(a) <- "lm"
    #' MyHappyFunction(a)
    #'
    #' @rdname MyHappyFunction
    #' @export MyHappyFunction
    MyHappyFunction <- function(x, ...){
      UseMethod("MyHappyFunction")
    }
    
    #' @return \code{NULL}
    #'
    #' @rdname MyHappyFunction
    #' @method MyHappyFunction lm
    #' @S3method MyHappyFunction lm
    MyHappyFunction.lm = function(x, ...) {
      # do some magic
    }
    
    #' @return \code{NULL}
    #'
    #' @rdname MyHappyFunction
    #' @method MyHappyFunction default
    #' @S3method MyHappyFunction default
    MyHappyFunction.default = function(x, ...) {
      # do some magic
    }
    

    enter image description here

    3 From the wiki page...

    I guess that it means "you do not write @S3method generic mymethod myobject."

    0 讨论(0)
提交回复
热议问题