Skip to content

Commit

Permalink
Manual delayed s3 registration
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasp85 committed Mar 3, 2019
1 parent 85de7e9 commit 268e138
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
8 changes: 0 additions & 8 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}

2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
32 changes: 32 additions & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
@@ -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))
}
)
}

0 comments on commit 268e138

Please sign in to comment.