Hello,
We are trying to build an R package with more than two S4 classes that have
common methods. However, when checking the methods part of the documentation
for the second class, we see only the definition of the generic of the first
class.
For illustration, we can call these two classes `Myclass` and `Mynewclass`
which have similarly named methods `parameters` and `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.
#' @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
#' @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.
#' @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
#' @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)
})
```
and
```
#' 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
#' @describeIn Mynewclass Retrieves parameters Mynewclass.
#'
#' @param object An object of the class Mynewclass.
#'
#' @return parameter values of Mynewclass.
#' @aliases parameters,ANY,ANY-method
#' @export
setMethod('parameters', 'Mynewclass',
function(object) object@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
#' @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)
})
```
It might be worth mentioning that keeping the generics for both classes led to
problems in the `devtools::document()` command, as seen in the following
StackOverflow question
[https://stackoverflow.com/questions/68113799/how-can-you-create-multiple-s4-classes-with-similarly-named-methods-in-r]
How could we solve this issue without having to give different names for the
methods?
Many thanks,
Ioana
PS. The link to the relevant branch of the Github repository is below:
[https://github.com/Como-DTC-Collaboration/como-models/tree/i4-age-structure]
[[alternative HTML version deleted]]
______________________________________________
[email protected] mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel