ReferenceClass的有效性检查

ReferenceClass的有效性检查,r,R,S4类允许您使用validObject()或setValidity()定义有效性检查。但是,这似乎不适用于ReferenceClass 我尝试将assert\u that()或if(badness)stop(message)子句添加到引用类的$initialize()方法中。但是,当我模拟加载包时(使用devtools::load_all()),它必须尝试创建一些原型类,因为initialize方法执行并失败(因为没有设置任何字段) 我做错了什么?好的,您可以在初始化中执行此操作。其格式应为:

S4类允许您使用
validObject()
setValidity()
定义有效性检查。但是,这似乎不适用于ReferenceClass

我尝试将
assert\u that()
if(badness)stop(message)
子句添加到引用类的
$initialize()
方法中。但是,当我模拟加载包时(使用
devtools::load_all()
),它必须尝试创建一些原型类,因为
initialize
方法执行并失败(因为没有设置任何字段)


我做错了什么?

好的,您可以在
初始化中执行此操作。其格式应为:

initialize = function (...) {
  if (nargs()) return ()
  # Capture arguments in list
  args <- list(...)
  # If the field name is passed to the initialize function
  # then check whether it is valid and assign it. Otherwise
  # assign a zero length value (character if field_name has 
  # that type) 
  if (!is.null(args$field_name)) { 
    assert_that(check_field_name(args$field_name))
    field_name <<- field_name  
  } else {
    field_name <<- character()
  }
  # Make sure you callSuper as this will then assign other 
  # fields included in ... that weren't already specially 
  # processed like `field_name`
  callSuper(...)
}
initialize=函数(…){
if(nargs())返回()
#捕获列表中的参数

args确定,因此您可以在
初始化中执行此操作。它应具有以下格式:

initialize = function (...) {
  if (nargs()) return ()
  # Capture arguments in list
  args <- list(...)
  # If the field name is passed to the initialize function
  # then check whether it is valid and assign it. Otherwise
  # assign a zero length value (character if field_name has 
  # that type) 
  if (!is.null(args$field_name)) { 
    assert_that(check_field_name(args$field_name))
    field_name <<- field_name  
  } else {
    field_name <<- character()
  }
  # Make sure you callSuper as this will then assign other 
  # fields included in ... that weren't already specially 
  # processed like `field_name`
  callSuper(...)
}
initialize=函数(…){
if(nargs())返回()
#捕获列表中的参数

args在引用类上实现有效性方法

A = setRefClass("A", fields=list(x="numeric", y="numeric"))

setValidity("A", function(object) {
    if (length(object$x) != length(object$y)) {
        "x, y lengths differ" 
    } else NULL
})
并显式调用validity方法

> validObject(A())
[1] TRUE
> validObject(A(x=1:5, y=5:1))
[1] TRUE
> validObject(A(x=1:5, y=5:4))
Error in validObject(A(x = 1:5, y = 5:4)) : 
  invalid class "A" object: x, y lengths differ

不幸的是,
setValidity()
需要作为initialize方法或构造函数的倒数第二行显式调用。

在引用类上实现一个validity方法

A = setRefClass("A", fields=list(x="numeric", y="numeric"))

setValidity("A", function(object) {
    if (length(object$x) != length(object$y)) {
        "x, y lengths differ" 
    } else NULL
})
并显式调用validity方法

> validObject(A())
[1] TRUE
> validObject(A(x=1:5, y=5:1))
[1] TRUE
> validObject(A(x=1:5, y=5:4))
Error in validObject(A(x = 1:5, y = 5:4)) : 
  invalid class "A" object: x, y lengths differ

不幸的是,
setValidity()
需要作为初始化方法或构造函数的倒数第二行显式调用。

我喜欢这种方法,因为它将丑陋的有效性检查代码移出了初始化方法。尽管我不确定这是否对我的解决方案有任何其他好处。我喜欢这种方法,因为它移动丑陋的有效性检查代码虽然我不确定这对我的解决方案是否有任何额外的优势。