🎓 A1Learner
🔍
0 of 33 lessons completed

R S3 Methods

A print method takes over the way print shows a value of its class. A summary method builds a summary value, and a print method for that summary class formats it.

print takes over for a class

print.wallet holds the text that print shows for a wallet. Calling print on the wallet shows that text and nothing else.

w <- list(balance = 50)
class(w) <- "wallet"
print.wallet <- function(x, ...) {
  cat("Wallet:", x$balance, "coins\n")
}
print(w)

A summary builds a new classed value

summary.team adds the scores and stores the total in a list with class summary.team. print.summary.team formats that list when the summary prints.

team <- list(name = "Ana", scores = c(10, 20, 30))
class(team) <- "team"
summary.team <- function(object, ...) {
  total <- sum(object$scores)
  result <- list(name = object$name, total = total)
  class(result) <- "summary.team"
  return(result)
}
print.summary.team <- function(x, ...) {
  cat("Team", x$name, "total:", x$total, "\n")
}
print(summary(team))

The summary class selects the print method

The class of the summary value is summary.team. Printing the summary then uses the print method for that class.

team <- list(name = "Bo", scores = c(5, 15))
class(team) <- "team"
summary.team <- function(object, ...) {
  total <- sum(object$scores)
  result <- list(name = object$name, total = total)
  class(result) <- "summary.team"
  return(result)
}
print.summary.team <- function(x, ...) {
  cat("Team", x$name, "total:", x$total, "\n")
}
print(class(summary(team)))
print(summary(team))

Example

w <- list(balance = 80)
class(w) <- "wallet"
print.wallet <- function(x, ...) {
  cat("Wallet:", x$balance, "coins\n")
}
team <- list(name = "Cy", scores = c(10, 10, 10))
class(team) <- "team"
summary.team <- function(object, ...) {
  total <- sum(object$scores)
  result <- list(name = object$name, total = total)
  class(result) <- "summary.team"
  return(result)
}
print.summary.team <- function(x, ...) {
  cat("Team", x$name, "total:", x$total, "\n")
}
print(w)
print(summary(team))

Key points

  • A method name joins a generic name, a dot, and a class name.
  • print.wallet runs when print is called on a value with class wallet.
  • The print method chooses the exact text that appears in the output.
  • summary.team runs when summary is called on a value with class team.
  • A summary method often builds a small list that holds the summary values.
  • Giving the summary list the class summary.team lets a second print method format it.
  • print(summary(team)) runs the summary method first and the summary print method second.
  • Each method definition carries ... so extra arguments can pass through the call.

In this example print shows the wallet through print.wallet and shows its chosen text. summary then builds a summary.team value for the team with a total of 30. print.summary.team formats that summary as one line.

Try it Yourself

Edit the code, then press Run.

Running is simulated · rdrr.io R Snippets

Exercise

What runs when print is called on a value with class wallet and print.wallet exists?