r - 如何使用 Roxygen2 使用单个帮助页面正确记录相同泛型的两个 S4 方法?
问题描述
我正在使用 Roxygen2 构建一个 R 包作为文档。我想为我的程序拥有的两个类中的每一个实现相同的 S4 方法。我不知道如何为他们正确设置文档以获得单个帮助页面。
我花了一天的时间试图为它找到好的配置,并且检查了多个帖子(例如a、b、c),但我仍然无法让它正常工作。
我的两个类如下所示:
#' @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"))
对于我想实现的两个类myMethod
。因此,按照本文的建议,我创建了一个虚拟文档文件,我在其中记录了 NULL,并设置了一个信息丰富的 @name。
虚拟文档文件如下所示:
#' @title myMethod
#' @description Runs myMethod.
#' @param object an object.
#' @name myMethod
NULL
尝试 1
这些方法的实现如下所示:
对于 1 类
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)
对于 2 类
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)
我可以检查我的包裹并且没有收到任何错误、警告或注释。但是,我想要一个帮助页面,myMethod
上面的结构给了我两个帮助页面:myMethod
和myMethod-method
. 请注意,在我想要的单个帮助页面中,我还希望有一个通用描述,即我在虚拟文件中编写的那个,而不是两个合并的描述。
我不知道应该如何设置文档以便获得一个myMethod
帮助页面。
尝试 2
除了上面的设置之外,我还尝试了这个 SO 答案中提出的结构,如下所示:
对于 1 类
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)
对于 2 类
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)
使用这种结构,我会得到一个myMethod
我想要命名的帮助页面。但是,在安装和重新启动期间,我还会收到“myMethod-methods.Rd 缺少名称/标题。跳过”错误。检查时没有问题。我不知道这种结构是否可行,但是为了消除错误,缺少一些东西。
感谢您的投入!