R S3 class: Decide between overwriting vs appending the class name of the class attribute

你离开我真会死。 提交于 2019-12-23 20:45:21

问题


I want to create an S3 class. How do I decide which way of setting the class attribute is the right one (since it makes a difference)?

1) Overwrite the class attribute

object <- data.frame(field1 = "a", field2 = 2)
class(object)
# [1] "data.frame"
class(object) <- "MyClass"    # set the class by overwriting the existing one
class(object)
# [1] "MyClass"

2) Append the class attribute

I could also append the class name (at the beginning or end):

object2 <- data.frame(field1 = "a", field2 = 2)
class(object2) <- append(class(object2), "MyClass")
class(object2)
# [1] "data.frame"    "MyClass"

object3 <- data.frame(field1 = "a", field2 = 2)
class(object3) <- append("MyClass", class(object3))
class(object3)
# [1] "MyClass"    "data.frame"

I know that appending the class name at the beginning vs. at the end potentially changes the function that is called (from ?class):

When a generic function fun is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function is found, a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used (if it exists).

E. g. if I define an overloaded function it is not always called:

print.MyClass <- function(x) { print("printing MyClass") }

print(object)
# [1] "printing MyClass"

print(object2)
#   field1 field2
# 1      a      2

print(object3)
# [1] "printing MyClass"

So my question is: How do I decide how to set the class name (which [other] criteria I have to consider)?


回答1:


With reference to data.frame:

  • Overwriting If you replace the class name with your own name then you must define your own methods for each generic that could be called with it unless the default method of that generic is ok. If your class is completely different than the data frame then this is what you want. This would be, for example, in the case that data.frame methods cannot be used on your objects.

  • Prepending If you prepend your class name to the class vector then when a generic is called with an object having your new class name then the generic will first look to see if you defined a method for it and if not it will call the data.frame method. This is what you want if you want to override certain functionality of data frame but make use of other functionality. For example, in the tibble package, the class vector for a tibble object is c("tbl_df", "tbl", "data.frame") and in the data.table package the class vector of a data.table object is c("data.table", "data.frame")

  • Appending Normally you don't want to place your class after the existing class. If you did then it would only get called if there were no data.frame method.



来源:https://stackoverflow.com/questions/45175988/r-s3-class-decide-between-overwriting-vs-appending-the-class-name-of-the-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!