How to use confidence functions with inherited S4 classes in R

Suppose you have one S4 class "A" and a subclass of "B", which has additional features. Each of them has its own validation checks - B should only check additional functions. Now, when initializing B, I would like to start with an object of class A, and then change it with additional functions. However, this creates problems, and I assume that I am violating R's assumptions somewhere in this example.

Here is the dummy code:

setClass(Class="A",
         representation=
         representation(x="numeric"),
         validity=
         function(object){stopifnot(x > 0)})

setMethod("initialize",
          signature(.Object="A"),
          function(.Object,
                   ...,
                   z){
              x <- get("z") + 1
              callNextMethod(.Object,
                             ...,
                             x=x)
          })

setClass(Class="B",
         contains="A",
         representation=
         representation(y="numeric"),
         validity=
         function(object){stopifnot(y > 0)})

setMethod("initialize",
          signature(.Object="B"),
          function(.Object,
                   ...,
                   bla){

              .Object <- callNextMethod(.Object,
                                        ...)

              .Object@y <- .Object@x + bla
              return(.Object)
          })

test <- new("B",
            z=4,
            bla=5)

If I try to create a test object, I get:

Error in stopifnot(x > 0): object 'x' not found

Do you know how I could do better?

Many thanks! Regards Daniel

+4
2

S4 , new(), , VIRTUAL, .

> validObject(new("A"))
Error in get("z") : argument "z" is missing, with no default

z ​​ initialize ( ) . , TRUE ( ) , , . "A"

.A <- setClass(Class="A",
    representation(x="numeric"),
    prototype(x=1),
    validity= function(object) {
        msg <- NULL
        if (length(object@x) != 1 || object@x <= 0)
            msg <- c(msg, "'x' must be length 1 and > 0")
        if (is.null(msg)) TRUE else msg
    })

( setClass() new() ).

> validObject(.A())
[1] TRUE

initialize ( - ),

A <- function(z, ...)
    .A(x=z+1, ...)

> A()
Error in initialize(value, ...) (from valid.R!7685pfr#2) : 
  argument "z" is missing, with no default
> A(1)
An object of class "A"
Slot "x":
[1] 2

, "B" " "!

+6

, :

.A <- setClass(Class="A",
               representation(x="numeric"),
               prototype(x=1),
               validity=
               function(object){
                   msg <- NULL
                   if (length(object@x) != 1 || object@x <= 0)
                       msg <- c(msg, "'x' must be length 1 and > 0")
                   if (is.null(msg)) TRUE else msg
               })

validObject(.A())

A <- function(z, ...)
{
    x <- z + 1
    .A(x=x, ...)
}

.B <- setClass(Class="B",
               representation(y="numeric"),
               prototype(y=2),
               contains="A",
               validity=
               function(object){
                   msg <- NULL
                   if (length(object@y) != 1 || object@y <= 0)
                       msg <- c(msg, "'y' must be length 1 and > 0")
                   if (is.null(msg)) TRUE else msg
               })

validObject(.B())

B <- function(bla, z, ...)
{
    obj <- A(z, ...)
    y <- obj@x + bla
    .B(obj, y=y, ...)
}

test <- B(z=4,
          bla=5)

! Daniel

+2

Source: https://habr.com/ru/post/1569715/


All Articles