Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically document all methods of an S4 generic, using roxygen2

Tags:

r

s4

roxygen2

I am writing an r package using roxygen2 for the documentation. I am having some trouble documenting S4 methods. I have defined a generic s4 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

like image 941
alko989 Avatar asked Jun 16 '14 14:06

alko989


1 Answers

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

like image 136
Joris Meys Avatar answered Oct 16 '22 03:10

Joris Meys