3

I'm building an R package using Roxygen2 for documentation. I want to implement the same S4 method for each of two classes my program has. I don't know how to properly set up the documentation for them in order to get a single help page.

I've spent the day trying to find the good configuration for it and I've checked multiple posts (e.g., a, b, c), but I'm still unable to get it to work properly.

My two classes look like this:

#' @title Class: Class1
#' @description Description for Class1.
#' @slot x an x.
#' @rdname Class1-class
#' @export
setClass("Class1", representation(x = "numeric"))
#' @title Class: Class2
#' @description Description for Class 2.
#' @slot y a y.
#' @slot z a z.
#' @rdname Class2-class
#' @export
setClass("Class2", representation(y = "numeric", z = "numeric"))

For both classes I want to implement myMethod. Thus, following the suggestion of this article I've created a dummy documentation file where I document NULL and I set up an informative @name.

The dummy documentation file looks like this:

#' @title myMethod
#' @description Runs myMethod.
#' @param object an object.
#' @name myMethod
NULL

Attempt 1

The implementation of the methods looks like this:

For Class1

if(!isGeneric("myMethod")) {setGeneric("myMethod", function(object) standardGeneric("myMethod"))}

#' @rdname myMethod
#' @export
setMethod("myMethod", "Class1", function(object) myMethod.Class1(object))

myMethod.Class1 <- function(object) return(5+object@x)

For Class2

if(!isGeneric("myMethod")) {setGeneric("myMethod", function(object) standardGeneric("myMethod"))}

#' @rdname myMethod
#' @export
setMethod("myMethod", "Class2", function(object) myMethod.Class2(object))

myMethod.Class2 <- function(object) return(object@y+object@z)

I can check my package and get no errors, warnings or notes. However, I want a single help page for myMethod and the structure above is giving me two help pages: myMethod and myMethod-method. Note that in the single help page I want, I expect also to have a single general description, the one that I've wrote in my dummy file, and not two merged descriptions.

I don't know how should I set up the documentation so that I obtain a single myMethod help page.

Attempt 2

In addition to the setup above, I've tried the structure proposed in this SO answer as follows:

For Class1

if(!isGeneric("myMethod")) {setGeneric("myMethod", function(object) standardGeneric("myMethod"))}

#' @rdname myMethod-methods
#' @aliases myMethod,Class1,Class1-method
setMethod("myMethod", "Class1", function(object) myMethod.Class1(object))

myMethod.Class1 <- function(object) return(5+object@x)

For Class2

if(!isGeneric("myMethod")) {setGeneric("myMethod", function(object) standardGeneric("myMethod"))}

#' @rdname myMethod-methods
#' @aliases myMethod,Class2,Class2-method
setMethod("myMethod", "Class2", function(object) myMethod.Class2(object))

myMethod.Class2 <- function(object) return(object@y+object@z)

With this structure I get a single help page named myMethod as I want. However, I also get a "myMethod-methods.Rd is missing name/title. Skipping" error during Install and Restart. No problems when checking. I don't know if this structure is the way to go but there is something missing in order to remove the error.

Appreciate your inputs!

djbetancourt
  • 347
  • 3
  • 11

0 Answers0