Managing class and their order
# result is of class character; it prints correctly
result <- "Je suis donc je pense"
class(result)
print(result)
# I add a new class but I keep character class. All is ok
# The order of classes is not important because there is no print() function for class ECFOCF
class(result) <- unique(append(class(result),"ECFOCF")))
class(result)
print(result)
result <- "Je suis donc je pense"
class(result) <- unique(append("ECFOCF", class(result)))
class(result)
print(result)
# I create a new function for class ECFOCF
# This function is used in priority whatever the ordre of classes
print.ECFOCF <- function(x) {cat("print.ECFOCF ", x)}
result <- "Je suis donc je pense"
print(result)
class(result) <- unique(append(class(result),"ECFOCF")))
class(result)
print(result)
result <- "Je suis donc je pense"
class(result) <- unique(append("ECFOCF", class(result)))
class(result)
print(result)
# I create a new function for class CF
# The function used in priority depends on the order of classes
print.CF <- function(x) {cat("print.CF ", x)}
result <- "Je suis donc je pense"
class(result) <- unique(append("ECFOCF", class(result)))
class(result)
print(result)
class(result) <- unique(append("CF", class(result)))
class(result)
print(result)
result <- "Je suis donc je pense"
class(result) <- unique(append("CF", class(result)))
class(result)
print(result)
class(result) <- unique(append("ECFOCF", class(result)))
class(result)
print(result)
Commentaires
Enregistrer un commentaire