Самый простой способ - использовать классы R3 R *.
Простой пример. Предположим, у нас есть новый тип данных - вектор, который мы хотели бы напечатать в виде строки через запятую.
x <- c(1, 2, 3)
# Assign a class attribute. The first element is the class of the object;
# the second element is the parent class
class(x) <- c("Foo", "numeric")
# Define the print method
print.Foo <- function(x) {
cat(paste(x, collapse = ", "), "\n")
}
# The method is called just like this
print(x)