From 268e138d88453fd8c007a8fa5831934535053d5a Mon Sep 17 00:00:00 2001 From: Thomas Lin Pedersen Date: Sun, 3 Mar 2019 21:57:15 +0100 Subject: [PATCH] Manual delayed s3 registration --- NAMESPACE | 8 -------- NEWS.md | 2 ++ R/zzz.R | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 R/zzz.R diff --git a/NAMESPACE b/NAMESPACE index e073231..20f6fef 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -171,11 +171,3 @@ importFrom(tweenr,tween_numeric) importFrom(tweenr,tween_state) importFrom(utils,modifyList) importFrom(utils,packageVersion) - -if (getRversion() >= "3.6.0") { - S3method(knitr::knit_print, 'gganim') - S3method(knitr::knit_print, 'gif_image') - S3method(knitr::knit_print, 'video_file') - S3method(knitr::knit_print, 'sprite_image') -} - diff --git a/NEWS.md b/NEWS.md index 4aa9626..17d1586 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # gganimate (development version) +* Fix registration of S3 methods for suggested packages + # gganimate 1.0.1 * Fix bug in transitions when the group aesthetic was late evalued (#262) diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..b69ee1d --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,32 @@ +.onLoad <- function(...) { + register_s3_method("knitr", "knit_print", "gganim") + register_s3_method("knitr", "knit_print", "gif_image") + register_s3_method("knitr", "knit_print", "video_file") + register_s3_method("knitr", "knit_print", "sprite_image") + + invisible() +} + +register_s3_method <- function(pkg, generic, class, fun = NULL) { + stopifnot(is.character(pkg), length(pkg) == 1) + stopifnot(is.character(generic), length(generic) == 1) + stopifnot(is.character(class), length(class) == 1) + + if (is.null(fun)) { + fun <- get(paste0(generic, ".", class), envir = parent.frame()) + } else { + stopifnot(is.function(fun)) + } + + if (pkg %in% loadedNamespaces()) { + registerS3method(generic, class, fun, envir = asNamespace(pkg)) + } + + # Always register hook in case package is later unloaded & reloaded + setHook( + packageEvent(pkg, "onLoad"), + function(...) { + registerS3method(generic, class, fun, envir = asNamespace(pkg)) + } + ) +}