Double dispatch of S3 method

September 17, 2018

When I tried to define an S3 class that contains multiple ggplot objects, I’ve faced the lessor-know mechanism of S3 method dispatch, double dispatch. Problem Take a look at this example. manyplot class contains many plots, and displays them nicely when printted. library(ggplot2) set.seed(100) d1 <- data.frame(x = 1:100, y = cumsum(runif(100))) d2 <- data.frame(x = 1:100, y = cumsum(runif(100))) plot_all <- function(...) { l <- lapply(list(...), function(d) ggplot(d, aes(x, y)) + geom_line()) l <- unname(l) class(l) <- "manyplot" l } print. ... Read more