8
votes

I am writing an package using for the documentation. I am having some trouble documenting S4 methods. I have defined a generic method (e.g. myGeneric) and few methods that implement it.

Question: Is there a way to automatically document all methods of a generic?

The manual solution is to add these two lines for each method (this would be for the first method in my dummy example):

##' @rdname myGeneric-methods
##' @aliases myGeneric,numeric,numeric,missing-method

Since I use a lot of methods, I have to add a lot to satisfy the R CMD check.


I read in Hadley's Advanced R about documenting S4 methods here that one can use @genericMethods:

Use @genericMethods in the generic documentation if you want an automated listing of all methods implemented for the generic.

I think this is not implemented in roxygen2, or implemented under another name. The only thing I found about the @genericMethods tag was in the roxygen3 github here, which was discontinued(?).


Just a dummy example, where I document only the generic and want all the implemented methods automatically listed:

##' @param object An object
##' @param data Numeric vector or data.frame
##' @param Fun Function. Default function is \code{sum}
##' @param ... Extra named arguments passed to FUN
##' @docType methods
##' @export 
setGeneric("myGeneric", function(object, data, FUN, ...)
           {standardGeneric ("myGeneric")} )

setMethod("myGeneric", c("numeric", "numeric", "missing"),
          function(object, data,...) { 
              myGeneric(object, data, sum,...) 
          })
setMethod("myGeneric", c("numeric", "data.frame", "missing"),
          function(object, data,...) { 
              myGeneric(object, as.vector(unlist(data), sum,...) 
          })
setMethod("myGeneric", c("numeric", "numeric", "function"),
          function(object, data, FUN,...) {
             ## Do something
          })

Any help will be really appreciated,

alex

1
I don't know any way to automatically document all methods of a generic (maybe when @genericMethods will be implemented). I use instead #' @describeIn myGeneric in front of all my specific method. This does not answer exactly your question but I think it's the only way to achieve it for the moment.jomuller
I searched in the official GitHub repo of roxygen and didn't founded any roclet that should do this job. I think that the complete roclet list is in /roxygen/R/rocled-rd.R.jomuller
I added an issue on GitHub.jomuller

1 Answers

9
votes

EDIT: Updated to reflect the preferred method for the latest version of roxygen2 - march 23, 2016

Actually, you can do this a lot easier with using @rdname in roxygen2 :

#' @param object An object
#' @param data Numeric vector or data.frame
#' @param Fun Function. Default function is \code{sum}
#' @param ... Extra named arguments passed to FUN
#' @rdname myGeneric
#' @export 
setGeneric("myGeneric", function(object, data, FUN, ...)
           {standardGeneric ("myGeneric")} )

#' @rdname myGeneric
setMethod("myGeneric", c("numeric", "numeric", "missing"),
          function(object, data,...) { 
              myGeneric(object, data, sum,...) 
          })

#' @rdname myGeneric
setMethod("myGeneric", c("numeric", "data.frame", "missing"),
          function(object, data,...) { 
              myGeneric(object, as.vector(unlist(data), sum,...) 
          })

#' @rdname myGeneric
setMethod("myGeneric", c("numeric", "numeric", "function"),
          function(object, data, FUN,...) {
             ## Do something
          })

Does the trick for me with roxygen2 version 5.0.1