【问题标题】:R add a class to a fieldR向字段添加一个类
【发布时间】:2019-07-11 12:30:13
【问题描述】:

我在 R 中创建了两个类:

library(methods)

Foo <- setClass(
    # Set the name for the class
    "Foo",

    # Define the slots
    slots = c(
              # THIS IS PROBABLY WRONG
              bar = "S4"
            ),
    )

还有

Bar <- setClass(
    # Set the name for the class
    "Bar",

    # Define the slots
    slots = c(
            a = "character"
            ),

    # Set the default values for the slots. (optional)
    prototype=list(
            a = "qwerty"
            ),
    )

我想说一些类似于Foo.bar &lt;- Bar() 的内容。我认为应该这样做:

# create a method to assign the value of a coordinate
setGeneric(name="addBar",
                       def=function(theObject)
                       {
                               standardGeneric("addBar")
                       }
                       )

setMethod(f="addBar",
                      signature="Foo",
                      definition=function(theObject)
                      {
                              theObject@bar<- Bar
                      }
                      )

然后我使用:

if (!interactive()) {
    result <- tryCatch({
           foo <- Foo()
           foo <- addBar(Foo)
        }
        ,

        warning = function(war) {
            print('A warning occurred')
            print(war)
        },

        error = function(err){
            print('An error occurred')
            print(err)
        }
        )
    print(result)
}

但是,如果我运行它,我会受到欢迎:

“Bar”类对象的赋值对于“Foo”类对象中的@'bar'无效; is(value, "S4") 不是 TRUE>

但是,当我打印 Bar 的类型时,我得到 S4。我尝试了多种不同的类型,但我的想法很新鲜。

如何将类对象分配给变量?

【问题讨论】:

    标签: r types s4


    【解决方案1】:

    Foo@bar的类应该是Bar,你应该先定义类Bar

    Bar <- setClass("Bar",
        slots = c(a = "character"),
        prototype=list(a = "qwerty")
        )
    
    
    Foo <- setClass("Foo", 
        slots = c(bar = "Bar")
        )
    

    请注意,一旦您创建了foo &lt;- Foo()Foobar 插槽已经初始化(尝试str(foo),所以我认为您不需要addBar 通用的。除非它应该按照你的描述做其他事情。

    另外,如果有的话,那么你应该如下定义你的方法:

    setMethod(f="addBar", signature="Foo",
              definition=function(theObject) {
                theObject@bar<- Bar()
               })
    

    (假设您只想用空的Bar() 对象初始化@bar 插槽)。如果你做theObject@bar &lt;- Bar,那么你将类生成器函数Bar分配给槽@bar,会得到一个错误:

    Error in (function (cl, name, valueClass)  : 
      assignment of an object of class “classGeneratorFunction” is not 
      valid for @‘bar’ in an object of class “Foo”; is(value, "Bar") is not TRUE
    

    最后,注意调用

    foo <- addBar(Foo)
    

    不做你认为它做的事。 Foo 是用于生成类foo 的新对象的函数。所以addBar(Foo) 尝试在classGeneratorFunction 类的对象上运行addBar 方法,这可能不是您想要的。

    【讨论】:

    • 非常感谢您的回答。我是 R 新手,真的很挣扎。我的具体意图是从另一个文件(bar)导入一个类对象,并在我的原始类(foo)中创建该对象类型的变量。你说我在定义 foo 之前必须先定义 bar,所以这是否意味着我的意图是不可能的?
    • “另一个文件”是什么意思?另外,这是否意味着 Bar 已经在其他地方定义了?那么你根本不需要定义 Bar 。也许您应该在问题中更具体地说明您要达到的目标。
    • 我有两个文件。在我的第一个文件中,我有 Foo. Foo 导入定义了 Bar 的文件并创建一个变量 bar。我的目标是从 python 复制动态导入功能。
    • 我强烈建议将这个想法更详细地描述为一个新问题。除非我弄错了,来自 Python 的动态导入功能或多或少是 R 中的默认功能。在 R 中你不能做什么需要实现新类?我并不是说没有这样的事情,只是也许你正试图用 Python 的方式在 R 中做事情,而用 R 的方式可以更容易地实现它们。
    • 感谢您的评论。我会这样做的。
    【解决方案2】:

    我换行了

    bar = "S4"
    

    bar = "Bar"
    

    这似乎解决了问题。

    【讨论】:

      猜你喜欢
      • 2013-09-13
      • 2022-01-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多