【问题标题】:How can I implement the length function for S4 classes?如何实现 S4 类的长度函数?
【发布时间】:2021-07-19 14:00:54
【问题描述】:

我想在 R 中为 S4 类实例实现 length() 函数。我会上课:

setClass("A", slots = c(ll = "list"))

然后,我想创建一个A类的实例inst并调用length(inst)

我之前的尝试都失败了,到目前为止,我只能为 S3 类找到一种方法。 (就像Formula 包一样here。)

【问题讨论】:

    标签: r s4


    【解决方案1】:

    假设您只想将您的 length() 方法委托给插槽“ll”。

    setMethod("length", signature = "A", definition = function(x) length(x@ll))
    

    【讨论】:

    • 谢谢你,我可以发誓这在我尝试时对我不起作用......但它有效!
    猜你喜欢
    • 2021-10-21
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 2019-09-16
    相关资源
    最近更新 更多