From f87fe290708b668d35338dfc9f6c208c31c3aee0 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Mon, 25 Jan 2016 13:00:37 -0600 Subject: [PATCH] Add plot.R6 method, which calls $plot on object. Closes #77 --- DESCRIPTION | 1 + NAMESPACE | 3 ++- NEWS | 6 ++++++ R/print.R | 10 ++++++++++ man/R6Class.Rd | 2 +- 5 files changed, 20 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 66e8b6f..c52e725 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -21,3 +21,4 @@ License: MIT + file LICENSE URL: https://github.com/wch/R6/ LazyData: true VignetteBuilder: knitr +RoxygenNote: 5.0.1 diff --git a/NAMESPACE b/NAMESPACE index 0a7909c..0482207 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,7 +1,8 @@ -# Generated by roxygen2 (4.1.1): do not edit by hand +# Generated by roxygen2: do not edit by hand S3method(format,R6) S3method(format,R6ClassGenerator) +S3method(plot,R6) S3method(print,R6) S3method(print,R6ClassGenerator) export(R6Class) diff --git a/NEWS b/NEWS index bc3b9d0..9ad08c0 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,9 @@ +R6 2.1.2.9000 +------------------------------------------------------------------------------- + +* The `plot` S3 method for R6 objects will call `$plot` on the object if + present. (#77) + R6 2.1.2 ------------------------------------------------------------------------------- diff --git a/R/print.R b/R/print.R index df6f892..72ed273 100644 --- a/R/print.R +++ b/R/print.R @@ -131,3 +131,13 @@ trim <- function(str, n = 60) { if (nchar(str) > n) paste(substr(str, 1, 56), "...") else str } + + +#' @export +plot.R6 <- function(x, ...) { + if (is.function(x$plot)) { + x$plot(...) + } else { + stop(paste0("No plot method defined for R6 class ", class(x)[1])) + } +} diff --git a/man/R6Class.Rd b/man/R6Class.Rd index 6c160ae..227ccde 100644 --- a/man/R6Class.Rd +++ b/man/R6Class.Rd @@ -1,4 +1,4 @@ -% Generated by roxygen2 (4.1.1): do not edit by hand +% Generated by roxygen2: do not edit by hand % Please edit documentation in R/r6_class.R \name{R6Class} \alias{R6}