问题描述
我正在尝试使用两个 S4 类 Myclass
和 Mynewclass
构建一个 R 包,它们具有类似命名的方法 parameters
和 parameters<-
#' Example docstring of Myclass
#'
#' @slot parameters_names names of the parameters which are used by the
#' model.
#' @slot parameters parameters which are used by the
#' model.
#'
#' @import tidyverse
#' @import reshape2
#' @importFrom methods new
#' @export Myclass
#'
Myclass <- setClass('Myclass',# slots
slots = c(
parameters_names = 'list',parameters = 'list'
),# prototypes for the slots,automatically set output and param
# names
prototype = list(
parameters_names = list('a','b'),parameters = vector(mode = "list",length = 2)
)
)
# Setter and getter methods for parameters
#' Retrieves parameters Myclass.
#'
#' @param object An object of the class Myclass.
#'
#' @return parameter values of Myclass.
#' @rdname Myclass-class
#' @export
setGeneric('parameters',function(object) standardGeneric('parameters'))
#' @describeIn Myclass Retrieves parameters Myclass.
#'
#' @param object An object of the class Myclass.
#'
#' @return parameter values of Myclass.
#' @aliases parameters,ANY,ANY-method
#' @rdname Myclass-class
#' @export
setMethod('parameters','Myclass',function(object) object@parameters)
#' Sets parameters of Myclass,#'
#' @param object An object of the class Myclass
#' @param value a named list of (a,b).
#'
#' @return Updated version of Myclass.
#' @rdname Myclass-class
#' @export
setGeneric(
'parameters<-',function(object,value){
standardGeneric('parameters<-')
})
#' @describeIn Myclass Sets parameters of Myclass.
#'
#' @param object An object of the class Myclass.
#' @param value a named list of (a,b).
#'
#' @return Updated version of Myclass.
#' @aliases parameters<-,ANY-method
#' @rdname Myclass-class
#' @export
setMethod(
'parameters<-',value) {
a = value$a
b = value$b
ic <- list(a,b)
# if all above tests are passed,assign the ic namelist to the object
object@parameters <- ic
return(object)
})
和
#' Example docstring of Mynewclass
#'
#' @slot parameters_names names of the parameters which are used by the
#' model.
#' @slot parameters parameters which are used by the
#' model.
#'
#' @import tidyverse
#' @import reshape2
#' @importFrom methods new
#' @export Mynewclass
#'
Mynewclass <- setClass('Mynewclass','b','c'),length = 3)
)
)
# Setter and getter methods for parameters
#' Retrieves parameters Mynewclass.
#'
#' @param object An object of the class Mynewclass.
#'
#' @return parameter values of Mynewclass.
#' @rdname Mynewclass-class
#' @export
setGeneric('parameters',function(object) standardGeneric('parameters'))
#' @describeIn Mynewclass Retrieves parameters Mynewclass.
#'
#' @param object An object of the class Mynewclass.
#'
#' @return parameter values of Mynewclass.
#' @aliases parameters,ANY-method
#' @rdname Mynewclass-class
#' @export
setMethod('parameters','Mynewclass',function(object) object@parameters)
#' Sets parameters of Mynewclass,#'
#' @param object An object of the class Mynewclass
#' @param value a named list of (a,b,c).
#'
#' @return Updated version of Mynewclass.
#' @rdname Mynewclass-class
#' @export
setGeneric(
'parameters<-',value){
standardGeneric('parameters<-')
})
#' @describeIn Mynewclass Sets parameters of Mynewclass.
#'
#' @param object An object of the class Mynewclass.
#' @param value a named list of (a,c).
#'
#' @return Updated version of Mynewclass.
#' @aliases parameters<-,ANY-method
#' @rdname Mynewclass-class
#' @export
setMethod(
'parameters<-',value) {
a = value$a
b = value$b
c = value$c
ic <- list(a,c)
# if all above tests are passed,assign the ic namelist to the object
object@parameters <- ic
return(object)
})
但是,每当我尝试使用 devtools::document()
时,都会出现以下错误消息
解决方法
尽量不要在两个文件中都调用 setGeneric
。
对 Mynewclass
的重复调用可能会创建一个新的泛型,并删除前一个泛型(包括其关联方法)。这为您留下了一个仅用于 {{1}} 的方法。
另见 https://stat.ethz.ch/pipermail/r-devel/2010-January/056396.html