如何使用 Roxygen2 通过单个帮助页面正确记录同一仿制药的两个 S4 方法?

问题描述 投票:0回答:1

我正在使用 Roxygen2 构建 R 包作为文档。我想为我的程序的两个类中的每一个实现相同的 S4 方法。我不知道如何正确设置他们的文档以获得单个帮助页面。

我花了一天的时间试图找到它的良好配置,并且检查了多个帖子(例如,abc),但我仍然无法让它正常工作.

我的两个班级是这样的:

#' @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 缺少名称/标题。正在跳过” 错误。检查时没有问题。我不知道这种结构是否可行,但为了消除错误,缺少一些东西。

感谢您的投入!

r r-package r-s4 roxygen2 roxygen
1个回答
0
投票

在名为

aaa.r
的文件中(或者基本上,文件名位于包中
/R
文件夹中任何其他文件之前),输入:

setGeneric("myMethod", function(object) standardGeneric("myMethod"))

然后,为每个类创建一个文件,

class1.r
class2.r
(您也可以将它们放在同一个文件中)。它们包含:

#' @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.r
的文件中,放入方法定义:

#' @title myMethod
#' @description Runs myMethod.
#' @param object an object.
#' @rdname myMethod
#' @exportMethod myMethod
setMethod("myMethod", "Class1", function(object) myMethod.Class1(object))
myMethod.Class1 <- function(object) return(5+object@x)

#' @rdname myMethod
#' @exportMethod myMethod
setMethod("myMethod", "Class2", function(object) myMethod.Class2(object))
myMethod.Class2 <- function(object) return(object@y+object@z)

我认为你的问题是为你的方法编写文档,然后以

NULL
行结尾,这通常用于与函数/方法无关的文档对象(例如,包附带的数据对象,或教程)。

© www.soinside.com 2019 - 2024. All rights reserved.