首页 > 解决方案 > 创建包含相同类型插槽的 S4 类

问题描述

作为实践,我正在使用 S4 类在 R 中创建 LinkedList 实现。

我有以下课程:

setClass("node",

  slots = list(
    value = "numeric",
    next_node = "node"
  ),

  prototype = list(
    value = NA_real_,
    next_node = NA
  ) 
)

但是,我收到以下错误消息:

Error in makePrototypeFromClassDef(properties, ClassDef, immediate, where) : 
  in making the prototype for class “node” elements of the prototype failed to match the corresponding slot class: next_node (class "node" )
In addition: Warning message:
undefined slot classes in definition of "node": next_node(class "node")

标签: roop

解决方案


您可以使用:

setClass("node",

  slots = list(
    value = "numeric",
    next_node = "nullOrNode"
  ),

  prototype = list(
    value = NA_real_,
    next_node = NULL
  ) 
)

setClassUnion("nullOrNode", c("NULL", "node"))

推荐阅读