如何在 R 中使用类似命名的方法创建多个 S4 类?
How can you create multiple S4 classes with similarly named methods in R?
我正在尝试用两个 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,ANY-method
#' @rdname Myclass-class
#' @export
setMethod(
'parameters<-', 'Myclass',
function(object, 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',
# 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', 'c'),
parameters = vector(mode = "list", 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,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<-',
function(object, value){
standardGeneric('parameters<-')
})
#' @describeIn Mynewclass 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.
#' @aliases parameters<-,ANY,ANY-method
#' @rdname Mynewclass-class
#' @export
setMethod(
'parameters<-', 'Mynewclass',
function(object, value) {
a = value$a
b = value$b
c = value$c
ic <- list(a, b, c)
# if all above tests are passed, assign the ic namelist to the object
object@parameters <- ic
return(object)
})
但是,每当我尝试使用 devtools::document()
时,都会出现以下错误消息
methods::getMethod(name, eval(call$signature), where = env) 错误:
未找到函数 'parameters' 和签名 Myclass
的方法
如何在不必为方法指定不同名称的情况下解决此问题?
尽量不要在两个文件中调用 setGeneric
。
可能是对 setGeneric
的重复调用创建了一个新的泛型,并删除了前一个泛型(包括其关联的方法)。这给您留下了一个仅适用于 Mynewclass
.
的方法
另请参阅
https://stat.ethz.ch/pipermail/r-devel/2010-January/056396.html
我正在尝试用两个 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,ANY-method
#' @rdname Myclass-class
#' @export
setMethod(
'parameters<-', 'Myclass',
function(object, 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',
# 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', 'c'),
parameters = vector(mode = "list", 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,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<-',
function(object, value){
standardGeneric('parameters<-')
})
#' @describeIn Mynewclass 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.
#' @aliases parameters<-,ANY,ANY-method
#' @rdname Mynewclass-class
#' @export
setMethod(
'parameters<-', 'Mynewclass',
function(object, value) {
a = value$a
b = value$b
c = value$c
ic <- list(a, b, c)
# if all above tests are passed, assign the ic namelist to the object
object@parameters <- ic
return(object)
})
但是,每当我尝试使用 devtools::document()
时,都会出现以下错误消息
methods::getMethod(name, eval(call$signature), where = env) 错误: 未找到函数 'parameters' 和签名 Myclass
的方法如何在不必为方法指定不同名称的情况下解决此问题?
尽量不要在两个文件中调用 setGeneric
。
可能是对 setGeneric
的重复调用创建了一个新的泛型,并删除了前一个泛型(包括其关联的方法)。这给您留下了一个仅适用于 Mynewclass
.
另请参阅 https://stat.ethz.ch/pipermail/r-devel/2010-January/056396.html