|
10 | 10 |
|
11 | 11 | .onLoad <- function(libname, pkgname) { |
12 | 12 | # ggvis provides methods for knitr::knit_print, but knitr isn't a Depends or |
13 | | - # Imports of htmltools, only a Suggests. This code snippet manually |
14 | | - # registers our method(s) with S3 once both ggvis and knitr are loaded. |
| 13 | + # Imports of ggvis, only a Suggests. This code snippet manually registers |
| 14 | + # our method(s) with S3 once both ggvis and knitr are loaded. |
15 | 15 | register_s3_method("knitr", "knit_print", "ggvis") |
16 | 16 | } |
17 | 17 |
|
18 | 18 | register_s3_method <- function(pkg, generic, class, fun = NULL) { |
19 | 19 | stopifnot(is.character(pkg), length(pkg) == 1) |
20 | | - envir <- asNamespace(pkg) |
21 | | - |
22 | 20 | stopifnot(is.character(generic), length(generic) == 1) |
23 | 21 | stopifnot(is.character(class), length(class) == 1) |
| 22 | + |
24 | 23 | if (is.null(fun)) { |
25 | 24 | fun <- get(paste0(generic, ".", class), envir = parent.frame()) |
| 25 | + } else { |
| 26 | + stopifnot(is.function(fun)) |
26 | 27 | } |
27 | | - stopifnot(is.function(fun)) |
28 | | - |
29 | 28 |
|
30 | 29 | if (pkg %in% loadedNamespaces()) { |
31 | | - registerS3method(generic, class, fun, envir = envir) |
| 30 | + registerS3method(generic, class, fun, envir = asNamespace(pkg)) |
32 | 31 | } |
33 | 32 |
|
34 | 33 | # Always register hook in case package is later unloaded & reloaded |
35 | 34 | setHook( |
36 | 35 | packageEvent(pkg, "onLoad"), |
37 | 36 | function(...) { |
38 | | - registerS3method(generic, class, fun, envir = envir) |
| 37 | + registerS3method(generic, class, fun, envir = asNamespace(pkg)) |
39 | 38 | } |
40 | 39 | ) |
41 | 40 | } |
0 commit comments